使用 Python 使用 Selenium 设置 chromedriver 代理身份验证 [英] Setting chromedriver proxy auth with Selenium using Python
问题描述
我正在使用 Python 和 Selenium 库编写测试套件.使用 chromedriver,我使用以下方法设置代理:
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)
当代理没有身份验证时,这可以正常工作.但是,如果代理要求您使用用户名和密码登录,它将不起作用.使用 add_argument 或其他方法将代理身份验证信息传递给 chromedriver 的正确方法是什么?
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?
它不同于:如何在 Selenium Java 中为 Chrome 设置代理设置
视为:
- 我是另一种语言
- 它是 Firefox,而不是 chrome.
- --proxy-server=http://user:password@proxy.com:8080 不起作用.
- I ts a different language
- Its firefox, not chrome.
- --proxy-server=http://user:password@proxy.com:8080 does not work.
推荐答案
使用 DesiredCapabilities
.我已成功使用以下代理身份验证:
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)
不幸的是,自这篇文章以来,此方法之一已更新为 Selenium 或 Chrome,因此似乎不再有效.截至目前,我不知道其他解决方案,但如果我发现任何问题,我会进行试验并更新.
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.
这篇关于使用 Python 使用 Selenium 设置 chromedriver 代理身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!