10

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.
6
  • stackoverflow.com/questions/30451190/… Commented Jun 12, 2016 at 15:12
  • @KirilS. Based on what was said in that topic I'll need some sort of extension in chrome to accomplish this? Commented Jun 12, 2016 at 15:16
  • the main thing it says is that unlike Firefox, Chrome uses OS proxy (not its own), so your options are 1 - settings the OS with proper proxy settings before test (good workaround for Windows where you can just setup a special user for selenium testing); 2 - setting OS proxy settings from the test (might be too complicated); 3 - using a special add-on that would allow you to change proxy settings on the fly. Commented Jun 12, 2016 at 15:20
  • @KirilS. On unix systems, I am getting: Message: unknown error: cannot process extension #1 from unknown error: invalid public key length Commented Jun 12, 2016 at 15:35
  • any update on this? this is killing me. Can it be done in Chrome? Commented Mar 4, 2017 at 18:42

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

it didn't work for me, I had to use a dynamically generated extension. I will try again in a while. I am using Selenium .NET, may be that is the reason
there is a typo 5 th line username is written as usernmae
This method does'nt work with chromedriver and proxy requiring authentication.
@Stack tagging you so you get notified as well
@anshaj unfortunatley not, and there probably wont be as the code has changed and likely wont be reverted. however, there is a work around by building a chrome extension through code. here is a link with details on how. look at the second section that is titled "HTTP Proxy Authentication with Chromedriver in Selenium"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.