Jorge
Jorge

Reputation: 345

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.

Upvotes: 10

Views: 20912

Answers (1)

crookedleaf
crookedleaf

Reputation: 2198

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.

Upvotes: 3

Related Questions