Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting chromedriver proxy auth with Selenium using Python

I am coding a test suite using Python and the Selenium library. Using the chromedriver, I am setting proxies using:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % hostname + ":" + port)
global driver
driver = webdriver.Chrome(chrome_options=chrome_options)

This works fine when the proxy does not have authentication. However, if the proxy requires you to login with a username and password it will not work. What is the correct and proper way to pass proxy authentication information to the chromedriver using add_argument or other methods?

It is not the same as: How to set Proxy setting for Chrome in Selenium Java

Seeing as:

  1. I ts a different language
  2. Its firefox, not chrome.
  3. --proxy-server=http://user:[email protected]:8080 does not work.
like image 951
Jorge Avatar asked Jun 12 '16 15:06

Jorge


People also ask

How does Selenium handle proxy?

You can handle proxy authentication popups using Selenium web driver by switching to the HTTP proxy authentication alert and passing the user name and password directly to the alert. With the help of send keys method.

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

Use DesiredCapabilities. I have been successfully using proxy authentication with the following:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

proxy = {'address': '123.123.123.123:2345',
         'username': 'johnsmith123',
         'password': 'iliketurtles'}


capabilities = dict(DesiredCapabilities.CHROME)
capabilities['proxy'] = {'proxyType': 'MANUAL',
                         'httpProxy': proxy['address'],
                         'ftpProxy': proxy['address'],
                         'sslProxy': proxy['address'],
                         'noProxy': '',
                         'class': "org.openqa.selenium.Proxy",
                         'autodetect': False}

capabilities['proxy']['socksUsername'] = proxy['username']
capabilities['proxy']['socksPassword'] = proxy['password']

driver = webdriver.Chrome(executable_path=[path to your chromedriver], desired_capabilities=capabilities)

EDIT: it unfortunately seems this method no longer works since one of the updated to either Selenium or Chrome since this post. as of now, i do not know another solution, but i will experiment and update this if i find anything out.

like image 117
crookedleaf Avatar answered Oct 04 '22 12:10

crookedleaf