I want to use selenium with a proxy which is password protected. The proxy is not fixed, but a variable. So this has to be done in the code (just setting up firefox on this particular machine to work with the proxy is less-than-ideal). So far I have the following code:
fp = webdriver.FirefoxProfile()
# Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5
fp.set_preference("network.proxy.type", 1)
fp.set_preference("network.proxy.http", PROXY_HOST)
fp.set_preference("network.proxy.http_port", PROXY_PORT)
driver = webdriver.Firefox(firefox_profile=fp)
driver.get("http://whatismyip.com")
At this point, the dialog pops up requesting the proxy user/pass.
Is there an easy way to either:
- Type in the user/pass in the dialog box.
- Provide the user/pass at an earlier stage.
Code worked for me
Did you try
PROXY_HOST = "http://username:password@proxy.host.com"
?Also:
Selenium can't do that by itself. The only way I found helpful is described here. To be short, you need to add a browser extension on fly that does the authentication. It's much simpler than may seem to be.
Here is how it works for Chrome (in my case):
background.js
Don't forget to replace YOUR_PROXY_* to your settings.
manifest.json
Add the created proxy.zip as an extension
That's it. For me that worked like a charm. If you need to create proxy.zip dynamically or need PHP example then go to the original post
After getting inspired by the unit tests in the selenium github repo. This worked for me: