简体   繁体   English

使用Python使用Selenium设置chromedriver代理auth

[英]Setting chromedriver proxy auth with Selenium using Python

I am coding a test suite using Python and the Selenium library. 我正在使用Python和Selenium库编写测试套件。 Using the chromedriver, I am setting proxies using: 使用chromedriver,我使用以下方法设置代理:

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? 使用add_argument或其他方法将代理身份验证信息传递给chromedriver的正确和正确方法是什么?

It is not the same as: How to set Proxy setting for Chrome in Selenium Java 它与以下内容不同: 如何在Selenium Java中为Chrome设置代理设置

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. --proxy-server = http:// user:password@proxy.com:8080不起作用。

Use DesiredCapabilities . 使用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. 编辑:不幸的是,这个方法似乎不再有效,因为自从这篇文章以来,其中一个更新为Selenium或Chrome。 as of now, i do not know another solution, but i will experiment and update this if i find anything out. 截至目前,我不知道另一种解决方案,但如果我发现任何问题,我将进行实验和更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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