我无法通过 Selenium Firefox WebDriver 使用代理进行连接。
使用此配置,生成连接但不是通过代理而是通过本地服务器。
关于这个问题有两个问题this documentation,但似乎没有人为python3解决了这个问题:
def selenium_connect():
proxy = "178.20.231.218"
proxy_port = 80
url = "https://www.whatsmyip.org/"
fp = webdriver.FirefoxProfile()
# Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5
fp.set_preference("network.proxy.type", 1)
fp.set_preference("network.proxy.http",proxy)
fp.set_preference("network.proxy.http_port",proxy_port)
fp.update_preferences()
driver = webdriver.Firefox(firefox_profile=fp)
driver.get(url)
我使用的是 Firefox webdriver 版本 52.0.2 和 Python 3.7 以及标准的 Ubuntu 16.04 Docker 环境。
最佳答案
您不需要使用 DesiredCapabilities 而不是在 FirefoxProfile 中设置代理吗?像下面这样。
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.proxy import Proxy
proxy_to_use= "xxx.xxx.xxx.xxx"
desired_capability = webdriver.DesiredCapabilities.FIREFOX
desired_capability['proxy'] = {
'proxyType': "manual",
'httpProxy': proxy_to_use,
'ftpProxy': proxy_to_use,
'sslProxy': proxy_to_use
}
browser = webdriver.Firefox(capabilities=desired_capability)
browser.get(“http://www.whatsmyip.org”)
关于python-3.x - 在 Python + Selenium + Firefox WebDriver 上配置代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55191100/