Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Proxy Settings without Closing the Driver in Selenium/Splinter

In an older version of Splinter/Selenium this was said not to be possible. This answer a few years later claims it is possible with JavaScript, but this code doens't work for me (I might have just failed to translate it to Python). This answer closes the browser and then re-opens it, and I need the window/browser to stay open.

With plugins like FoxyProxy, its very easy to change the proxy on-the-fly, but I don't think Selenium can interact with plugins because they are page elements?

As Splinter is designed to be a less verbose wrapper for Selenium, it would be awesome if there was an easy way to accomplish this. That being said, any hack to just have this functionality would be appreciated.

like image 331
jonbon Avatar asked May 10 '18 09:05

jonbon


People also ask

Can we handle a proxy using Selenium in Java?

We can handle proxy in Selenium in Java with the help of PROXY class.

Does Selenium act as a proxy server?

The leading web framework, Selenium, may configure your proxy server to support the testing requirements. If Selenium powers the test, then an HTTP request will generate a browser. The HTTP request runs several proxies at a time and will send the localized results.


1 Answers

You need to use it like below

browser.visit("about:config")

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

prefs.setIntPref("network.proxy.type", 1);
prefs.setCharPref("network.proxy.http", "{0}");
prefs.setIntPref("network.proxy.http_port", "{1}");
prefs.setCharPref("network.proxy.ssl", "{0}");
prefs.setIntPref("network.proxy.ssl_port", "{1}");
prefs.setCharPref("network.proxy.ftp", "{0}");
prefs.setIntPref("network.proxy.ftp_port", "{1}");
"""

browser.execute_script(script.format("ProxyIP", "PORT"))

PS: Credits to Python Selenium Webdriver - Changing proxy settings on the fly

like image 51
Tarun Lalwani Avatar answered Sep 28 '22 06:09

Tarun Lalwani