首页 > 解决方案 > Firefox with selenium and python proxy issues

问题描述

I hope I find you well today.

Background Information: Today I was coding a program and wanted to add proxy support. So far I have managed to connect using Localhost. However I want to add support for proxies that have a username and password (Format is IP Address:Port:Username:Password) in order to make as many accounts as possible. So far the code I have used is:

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()

Now this is only for 1 proxy and it isn't headless (I know). I wanted to see what happened before I made it headless.

What Happens: It opens the Firefox browser just fine and goes to the site as well. However it doesn't actually use the proxy. It just uses my localhost.

What I need: I need it to use the proxy. Also would like it to be able to pick up proxies from a text file and use them

标签: pythonseleniumproxy

解决方案


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

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

推荐阅读