Firefox与selenium和python代理问题

问题描述 投票:0回答:1

我希望我今天能找到你。

背景信息:今天我编写了一个程序并希望添加代理支持。到目前为止,我已设法使用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浏览器就好了,也进入了网站。但是它实际上并没有使用代理。它只使用我的localhost。

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

python selenium proxy
1个回答
0
投票

以下方法可用于获取具有代理的驱动程序

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
© www.soinside.com 2019 - 2024. All rights reserved.