如何使用 Python + Selenium 设置代理身份验证(用户和密码)
我在 Python 2.7 和 Selenium 中使用 Firefox WebDriver.当我运行该程序时,我的 Python 程序会启动 Firefox 浏览器并访问不同的网站.但是,我需要设置带有身份验证的代理,这样当程序访问任何网站时,它都会通过代理服务器访问.
I am using Firefox WebDriver in Python 2.7 with Selenium. My python program starts Firefox browser and visits different websites when I run the program. But, I need to set the proxy with authentication, so that when program visits any website, it will visit through the proxy server.
SO 上有一些类似的问题.但是,Python 的 Selenium Firefox WebDriver 没有具体的解决方案.
There are some similar questions on SO. But, there is no specific solution for Selenium Firefox WebDriver of Python.
Dup of: 如何使用 Python/Selenium 设置代理身份验证用户名:密码
硒线:https://github.com/wkeeling/selenium-wire
安装 selenium-wire
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 缓存文件夹.我遇到了问题,因为它占用了我所有的磁盘空间.有时您必须根据需要在脚本中将其删除.
Warning
Take a look to the selenium-wire cache folder. I had a problem because it take all my disk space. You have to remove it sometimes in your script when you want.