Changing the proxy server during Selenium

半世苍凉 提交于 2019-12-02 07:11:17

问题


So everything works

fp = webdriver.FirefoxProfile()
fp.set_preference("network.proxy.type", 1)
fp.set_preference("network.proxy.http", PROXY_HOST)
fp.set_preference("network.proxy.http_port", int(PROXY_PORT))
fp.update_preferences()
driver = webdriver.Firefox(firefox_profile=fp)

But if the driver has already been created, the proxy can not install. It does not work

driver = webdriver.Firefox()
driver.profile.set_preference("network.proxy.type", 1)
driver.profile.set_preference("network.proxy.http", PROXY_HOST)
driver.profile.set_preference("network.proxy.http_port", int(PROXY_PORT))
driver.profile.update_preferences()

And so, too.

 driver = webdriver.Firefox()
 driver.firefox_profile.set_preference("network.proxy.type", 1)
 driver.firefox_profile.set_preference("network.proxy.http", PROXY_HOST)
 driver.firefox_profile.set_preference("network.proxy.http_port", int(PROXY_PORT))
 driver.firefox_profile.update_preferences()

Why? Can not understand. I'm doing something wrong?


回答1:


When using WebDriver with Firefox, the use of the profile is a one-time thing. When the driver launches the browser, it writes the profile object to disk, then starts the browser executable. After that point, there is no mechanism for the browser to read any further changes to the WebDriver profile object. To change the proxy, you have to set the settings in the profile before the browser is launched.



来源:https://stackoverflow.com/questions/39791242/changing-the-proxy-server-during-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!