简体   繁体   中英

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:password@proxy.com:8080 does not work.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM