python - 如何在 selenium chromedriver 中使用经过身份验证的代理?

标签 python google-chrome selenium selenium-chromedriver

搜索了好几个小时后,我开始认为这是不可能的。

我需要为每次运行使用不同的经过身份验证的(非公共(public))代理通过 selenium 运行 Chrome。

PROXY_IP = "<some IP address>"
UID = "<the user id>"
PWD = "<the password">

options = webdriver.ChromeOptions()
options.add_argument("--proxy-server=%s:%s@%s" % (UID,PWD,PROXY_IP))

driver = webdriver.Chrome(executable_path=".\\driver\\chromedriver.exe",
                          chrome_options=options)
driver.get("<site URL>")

Chrome 将启动并显示错误:

This webpage is not available
ERR_NO_SUPPORTED_PROXIES

如果我使用像这样不需要身份验证的公共(public)代理...

PROXY_IP = "<public proxy IP address>"

options = webdriver.ChromeOptions()
options.add_argument("--proxy-server=%s" % PROXY_IP)

driver = webdriver.Chrome(executable_path=".\\driver\\chromedriver.exe",
                          chrome_options=options)
driver.get("<site URL>")

...它运行良好并在使用代理时显示站点。

我还尝试了在用户 ID 前添加 http:// 的变体:

options.add_argument("--proxy-server=http://%s:%s@%s" % (UID,PWD,PROXY_IP))

我进行了广泛的搜索,但没有找到解决方案,这让我相信根本不存在。

我确实找到了这个,但我无法理解它:

selenium chromedriver authentication proxy

不确定 browswermob-proxy 是什么或应该做什么或如何在 Python 中实现和测试。除非绝对必要,否则我讨厌堆积创可贴解决方案。

编辑(21 年 11 月 8 日):

我已经多年不使用 Selenium 了。正因为如此,我现在缺乏上下文(和时间,对不起)来检查提供的较新答案并将一个标记为该问题的解决方案。 SO 是否有一种机制可以用来有效地将这一职能委托(delegate)给可能是该领域专业知识的当前从业者?

最佳答案

要在 python selenium 中使用带有身份验证的代理,您可以使用 seleniumwire .

首先,使用 pip install selenium-wire

安装它

然后从 seleniumwire 导入 webdriver 而不是 selenium

from seleniumwire import webdriver
options = {
    'proxy': {
        'http': 'http://username:password@host:port', 
        'https': 'https://username:password@host:port',
        'no_proxy': 'localhost,127.0.0.1' # excludes
    }
}
browser = webdriver.Chrome(path_to_driver, seleniumwire_options=options)

现在您可以像使用 selenium 一样使用浏览器实例:browser.get('https://api.ipify.org') 等等...

关于python - 如何在 selenium chromedriver 中使用经过身份验证的代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30451190/

相关文章:

javascript - 拖动任何devexpress组件时如何防止其他元素突出显示?

python - 属性错误 : 'unicode' object has no attribute 'sleep'

selenium - 水貂 + PhantomJS : How do I set the user agent?

java - Xpath 无法运行(在 Eclipse 中),但可以在 Debug模式以及 Selenium IDE 下运行

python - PyDrive 如何使用 ID 下载文件夹

python - 根据条件保留 pandas 数据框的前 n 行

javascript - 为什么这个 html5 音频在 chrome 上不起作用,但在 safari 上运行良好

google-chrome - 为什么 Google Chrome 无法识别我的 SameSite cookie?

Python Pyinstaller ERRNO 22 无效参数

python - 解析并创建一个 dict python