如何使用 Python/Selenium 设置代理身份验证用户名:密码

Faz*_*lut 6 python firefox proxy selenium

我在用:

  • 硒 4
  • 蟒蛇 3.7
  • 火狐网络驱动程序

我需要验证当前项目的用户名和密码。我已经日以继夜地工作了几天,我无法解决这个问题。我在互联网上找到了一些扩展,但它是 13 年的,我想这就是它不起作用的原因。您还可以在下面看到我尝试过的其他方法。

但不起作用,因为由于 Selenium 错误,代理身份验证不再起作用。

alert_popup = browser.switch_to_alert()
    alert_popup.send_keys(
        "{username}{tab}{password}{tab}".format(
            username=proxy_username, tab=Keys.TAB, password=proxy_password
    )
)
alert_popup.accept()
Run Code Online (Sandbox Code Playgroud)

我想创建一个配置文件并手动保存代理信息并使用该配置文件启动 selenium。但是,firefox 不允许手动输入用户名和密码。

Faz*_*lut 5

我们用 Selenium 库解决了这个问题。希望它有益于您的业务。硒线

https://github.com/wkeeling/selenium-wire

并感谢@pguardiario 的帮助..


Tah*_*ssi 2

要在 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)
Run Code Online (Sandbox Code Playgroud)

  • 经过大量时间搜索为什么 selenium 加载页面非常慢(15 秒),结果发现问题来自 seleniumwire,如果您的操作对时间敏感,请寻找另一个解决方案 (3认同)