Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Selenium webdriver在Firefox中动态更改代理_Firefox_Selenium_Proxy - Fatal编程技术网

使用Selenium webdriver在Firefox中动态更改代理

使用Selenium webdriver在Firefox中动态更改代理,firefox,selenium,proxy,Firefox,Selenium,Proxy,在使用SeleniumWebDriver时,有没有办法动态更改Firefox使用的代理 目前我有使用代理配置文件的代理支持,但是当浏览器处于活动状态并运行时,是否有方法更改代理 我当前的代码: proxy = Proxy({ 'proxyType': 'MANUAL', 'httpProxy': proxy_ip, 'ftpProxy': proxy_ip, 'sslProxy': proxy_ip, 'noProxy': '' # set this va

在使用SeleniumWebDriver时,有没有办法动态更改Firefox使用的代理

目前我有使用代理配置文件的代理支持,但是当浏览器处于活动状态并运行时,是否有方法更改代理

我当前的代码:

proxy = Proxy({
    'proxyType': 'MANUAL',
    'httpProxy': proxy_ip,
    'ftpProxy': proxy_ip,
    'sslProxy': proxy_ip,
    'noProxy': '' # set this value as desired
    })
browser = webdriver.Firefox(proxy=proxy)

提前感谢。

据我所知,只有两种方法可以更改代理设置,一种是通过配置文件(您正在使用),另一种是在根据实例化驱动程序时使用驱动程序的功能。可悲的是,这两种方法都不能满足您的需要,因为它们都是在您创建驱动程序时出现的


我不得不问,你为什么要更改代理设置?我能想到的唯一解决方案是将firefox指向一个可以在运行时更改的代理。我不确定,但browsermob proxy可能会实现这一点。

据我所知,只有两种方法可以更改代理设置,一种是通过配置文件(您正在使用),另一种是在根据进行实例化时使用驱动程序的功能。可悲的是,这两种方法都不能满足您的需要,因为它们都是在您创建驱动程序时出现的


我不得不问,你为什么要更改代理设置?我能想到的唯一解决方案是将firefox指向一个可以在运行时更改的代理。我不确定,但使用browsermob代理可能会实现这一点。

一个可能的解决方案是关闭webdriver实例,并在每次操作后通过在浏览器配置文件中传递新配置再次创建它。

一个可能的解决方案是关闭webdriver实例,并在每次操作后通过传递新配置再次创建它浏览器配置文件中的配置这是一个稍微陈旧的问题。 但实际上,可以通过“黑客方式”动态更改代理 我将在Firefox中使用Selenium JS,但您可以使用您想要的语言

步骤1:访问“关于:配置”

步骤2:运行更改代理的脚本

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);
其中use${abcd}是放置变量的地方,在上面的示例中,我使用ES6处理连接,如图所示,您可以使用您选择的其他连接方法,具体取决于您的语言

第3步::访问您的网站

driver.get("http://whatismyip.com");

说明:上面的代码利用Firefox的API使用JavaScript代码更改首选项。

这是一个稍微陈旧的问题。 但实际上,可以通过“黑客方式”动态更改代理 我将在Firefox中使用Selenium JS,但您可以使用您想要的语言

步骤1:访问“关于:配置”

步骤2:运行更改代理的脚本

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);
其中use${abcd}是放置变量的地方,在上面的示例中,我使用ES6处理连接,如图所示,您可以使用您选择的其他连接方法,具体取决于您的语言

第3步::访问您的网站

driver.get("http://whatismyip.com");
说明:以上代码利用Firefox的API使用JavaScript代码更改首选项。

试试看,它甚至可以覆盖头字段

from seleniumwire import webdriver 

options = {
    'proxy': {
          "http": "http://" + IP_PORT, 
          "https": "http://" + IP_PORT,
          'custom_authorization':AUTH
          },
    'connection_keep_alive': True,
    'connection_timeout': 30,
    'verify_ssl': False 
}

# Create a new instance of the Firefox driver
driver = webdriver.Firefox(seleniumwire_options=options)
driver.header_overrides = {
    'Proxy-Authorization': AUTH
}

# Go to the Google home page
driver.get("http://whatismyip.com")
driver.close()
试一试,它甚至可以覆盖标题字段

from seleniumwire import webdriver 

options = {
    'proxy': {
          "http": "http://" + IP_PORT, 
          "https": "http://" + IP_PORT,
          'custom_authorization':AUTH
          },
    'connection_keep_alive': True,
    'connection_timeout': 30,
    'verify_ssl': False 
}

# Create a new instance of the Firefox driver
driver = webdriver.Firefox(seleniumwire_options=options)
driver.header_overrides = {
    'Proxy-Authorization': AUTH
}

# Go to the Google home page
driver.get("http://whatismyip.com")
driver.close()

也适用于Python。只需使用execute_脚本并将您的setupScript用“”引号括起来。@Joincandada您能提供您的代码段吗?也谢谢Python的SWORKS。只需使用execute_脚本并将您的setupScript用“”引号括起来。@Joincandada您能提供您的代码段吗?谢谢