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:password@proxy.com:8080 does not work.
Community
  • 1
  • 1
Jorge
  • 315
  • 2
  • 3
  • 11
  • http://stackoverflow.com/questions/30451190/how-to-use-authenticated-proxy-in-selenium-chromedriver – timbre timbre Jun 12 '16 at 15:12
  • @KirilS. Based on what was said in that topic I'll need some sort of extension in chrome to accomplish this? – Jorge Jun 12 '16 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. – timbre timbre Jun 12 '16 at 15:20
  • @KirilS. On unix systems, I am getting: Message: unknown error: cannot process extension #1 from unknown error: invalid public key length – Jorge Jun 12 '16 at 15:35
  • any update on this? this is killing me. Can it be done in Chrome? – Toolkit Mar 04 '17 at 18:42
  • @Toolkit check the answer i just posted and see if it works for you – crookedleaf Mar 06 '17 at 23:10

1 Answers1

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.

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
crookedleaf
  • 2,118
  • 4
  • 16
  • 38
  • 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 – Toolkit Mar 07 '17 at 11:07
  • 2
    there is a typo 5 th line `username` is written as `usernmae` – Stack Jun 25 '17 at 10:01
  • 3
    This method does'nt work with chromedriver and proxy requiring authentication. – Stack Jun 25 '17 at 10:03
  • This method doesnt forward username and password to chrome. Is there any other solution on this? – Alexey Trofimov Feb 19 '18 at 11:10
  • @AlexeyTrofimov 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. – crookedleaf Feb 19 '18 at 18:40
  • 1
    @Stack tagging you so you get notified as well – crookedleaf Feb 19 '18 at 18:41
  • 3
    @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](https://botproxy.net/docs/how-to/setting-chromedriver-proxy-auth-with-selenium-using-python/). look at the second section that is titled "HTTP Proxy Authentication with Chromedriver in Selenium" – crookedleaf Aug 09 '18 at 16:58