我在 Python 2.7 中使用 Firefox WebDriver 和 Selenium。我的 python 程序启动 Firefox 浏览器并在运行该程序时访问不同的网站。但是,我需要设置带有身份验证的代理,这样当程序访问任何网站时,都会通过代理服务器访问。
SO上有一些类似的问题。但是,Python的Selenium Firefox WebDriver没有具体的解决方案。
最佳答案
重复:How to set proxy AUTHENTICATION username:password using Python/Selenium
Selenium 线:https://github.com/wkeeling/selenium-wire
安装 Selenium 线
pip install selenium-wire
导入
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,dev_server:8080'
}
}
driver = webdriver.Firefox(seleniumwire_options=options)
警告
查看 selenium-wire 缓存文件夹。我遇到了问题,因为它占用了我所有的磁盘空间。有时您需要在脚本中删除它。
关于python - 如何使用 Python + Selenium 设置代理身份验证(用户和密码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38304253/