【问题标题】:Firefox with selenium and python proxy issues带有 selenium 和 python 代理问题的 Firefox
【发布时间】:2019-01-24 09:19:44
【问题描述】:

我希望我今天能找到你。

背景信息: 今天我正在编写一个程序并想添加代理支持。到目前为止,我已经设法使用 Localhost 进行连接。但是,我想添加对具有用户名和密码(格式为 IP 地址:端口:用户名:密码)的代理的支持,以便创建尽可能多的帐户。到目前为止我使用的代码是:

from selenium import webdriver

PROXY_HOST = "107.178.214.243"
PROXY_PORT = "3128"
USERNAME = "test" 
PASSWORD = "test"

profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", PROXY_HOST)
profile.set_preference("network.proxy.http_port", PROXY_PORT)
profile.set_preference("network.proxy.socks_username", USERNAME)
profile.set_preference("network.proxy.socks_password", PASSWORD)

profile.update_preferences()

# executable_path  = define the path if u don't already have in the PATH 
system variable. 
browser = webdriver.Firefox(firefox_profile=profile)
browser.get('https://whatismyipaddress.com/')
browser.maximize_window()

现在这仅适用于 1 个代理,它不是无头的(我知道)。我想看看在我让它无头之前发生了什么。

会发生什么:它可以很好地打开 Firefox 浏览器并访问该站点。但是它实际上并不使用代理。它只使用我的本地主机。

我需要什么:我需要它来使用代理。还希望它能够从文本文件中获取代理并使用它们

【问题讨论】:

  • 感谢您的帮助。但是浏览该帖子,在我看来,他们没有为经过身份验证的代理(具有用户名和密码的代理)提供解决方案

标签: python selenium proxy


【解决方案1】:

以下方法可用于通过代理获取驱动程序

def get_driver(PROXY):

firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX

firefox_capabilities['proxy'] = {"proxyType": "MANUAL", "httpProxy": PROXY, "ftpProxy": PROXY, "sslProxy": PROXY }
fp = webdriver.FirefoxProfile()

options = Options()
options.add_argument("--headless")

fp.update_preferences()
driver  = webdriver.Firefox(firefox_options=options,capabilities=firefox_capabilities, executable_path=geckodriver_path,firefox_profile=fp)
driver.set_window_size(2400,1980)

return driver

【讨论】:

    猜你喜欢
    • 2013-09-14
    • 2022-08-14
    • 1970-01-01
    • 2016-03-12
    • 2018-04-23
    • 2018-06-11
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    相关资源
    最近更新 更多