python-3.x - 在 Python + Selenium + Firefox WebDriver 上配置代理

标签 python-3.x selenium selenium-webdriver selenium-firefoxdriver

我无法通过 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/

相关文章:

python - 元组和字典的优先队列

python - 找不到程序 : 'pypy' on Google Colab. 解决方案或替代方案?

python - Selenium 意外出现问题

java - 如何使用机器人类在 Selenium 中基于 webelement 而不是 getlocation 的 xy 坐标执行 mousemove?

java - 获取 Groovy 项目中使用的 Selenium 版本

python - 如何删除整数列表中的最后一个逗号?

python - 使用至少 n 个空格拆分字符串

RS Selenium : replacing xpath with variable in R

c# - 如何设置 selenium 3.0,在 C# 中出现错误 "The geckodriver.exe file does not exist..."

python - 在 selenium 中使用 python 循环