如何在单个会话上多次更改Webdriver上的代理? [英] How to change proxy on my webdriver multiple times on a single session?

查看:98
本文介绍了如何在单个会话上多次更改Webdriver上的代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究机器人.我希望该漫游器每50次搜索更改一次webdriver的代理.我有一个请求代理和套接字的API,我存储了这些变量,到目前为止,我一直在使用firefox配置文件进行设置,但效果不佳.

I am working on a bot. I want the bot to change the proxy of the webdriver every 50 searches. I have an API that requests the proxy and the socket, i store those variables and so far i have been using firefox profiles to set it up but that doesnt work very well.

因此,鉴于我已经有了一个可行的代理和端口来源,您能告诉我可以在不使webdriver崩溃并在单个会话中完成的情况下更改代理的任何方法吗?

So given the fact that i already have a viable source of proxies and ports, can you tell me any way i can change the proxy without crashing the webdriver and doing it on a single session?

以前的尝试:

我尝试通过以下方式设置Firefox配置文件:

I tried setting up a firefox profile this way:

regions = {
    'US': '', #USA is the default server
    'Australia': #json response through the api,
    'Canada': #json response through the api,
    'France': #json response through the api,
    'Germany': #json response through the api,
    'UK': #json request response the api
}    
for region in regions:
        fp = webdriver.FirefoxProfile()
        if(regions[region] != ''):
            fp.set_preference("network.proxy.type", 1)
            fp.set_preference("network.proxy.socks", regions[region])
            fp.set_preference("network.proxy.socks_port", port)

这给我带来了一些问题,每次我想交换代理时我都必须开始一个新的会话.因此,我尝试通过Firefox选项(选项-常规-连接设置)更改代理,但结果是无法通过Selenium或javascript(xul文件)访问单击连接设置按钮后出现在屏幕上的弹出窗口.

This caused me some problems and i had to start a new session each time i wanted to swap a proxy. So i tried to change the proxy through the Firefox options (options - general - connection settings) but turns out the popup that appears on the screen after clicking the connection settings button is not accessible through selenium or javascript (xul file).

推荐答案

根据本主题,这是您的解决方案:

According to this topic, here is your solution :

解决方案链接: Python Selenium Webdriver-快速更改代理设置

var setupScript=`var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);

prefs.setIntPref("network.proxy.type", 1);
prefs.setCharPref("network.proxy.http", "${proxyUsed.host}");
prefs.setIntPref("network.proxy.http_port", "${proxyUsed.port}");
prefs.setCharPref("network.proxy.ssl", "${proxyUsed.host}");
prefs.setIntPref("network.proxy.ssl_port", "${proxyUsed.port}");
prefs.setCharPref("network.proxy.ftp", "${proxyUsed.host}");
prefs.setIntPref("network.proxy.ftp_port", "${proxyUsed.port}");
                  `;    

//running script below  
driver.executeScript(setupScript);

//sleep for 1 sec
driver.sleep(1000);

这篇关于如何在单个会话上多次更改Webdriver上的代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆