在Python + Selenium + Firefox WebDriver中配置代理

6

我无法通过Selenium Firefox WebDriver使用代理连接。

使用这种配置,连接是生成的,但不是通过代理而是本地服务器。

关于这个问题有两个问题和这份文档,但似乎都没有解决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环境。

2个回答

1

您需要使用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”)

-2

我通过在Windows级别迭代代理来解决这个问题,而不是使用Selenium。

通过PUTTY以编程方式重新配置SSH连接,可以为整个会话创建隧道。需要更多的前期设置,但更加可靠。

我使用像AppRobotic这样具有紧密Windows集成的工具,但任何好的宏或RPA产品都应该可以。我使用Python绑定与Selenium一起使用,但使用VBScript更新Windows配置。

主要的AppRobotic脚本用Python编写,可以在每次迭代中内联运行代理配置更新脚本,同时自动化其他应用程序,例如迭代Excel或Notepad行并在浏览器中执行某些操作,因为VBScript脚本可以被视为单独的“宏”。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接