Running Selenium Webdriver with a proxy in Python Running Selenium Webdriver with a proxy in Python selenium selenium

Running Selenium Webdriver with a proxy in Python


Works for me this way (similar to @Amey and @user4642224 code, but shorter a bit):

from selenium import webdriverfrom selenium.webdriver.common.proxy import Proxy, ProxyTypeprox = Proxy()prox.proxy_type = ProxyType.MANUALprox.http_proxy = "ip_addr:port"prox.socks_proxy = "ip_addr:port"prox.ssl_proxy = "ip_addr:port"capabilities = webdriver.DesiredCapabilities.CHROMEprox.add_to_capabilities(capabilities)driver = webdriver.Chrome(desired_capabilities=capabilities)


How about something like this

PROXY = "149.215.113.110:70"webdriver.DesiredCapabilities.FIREFOX['proxy'] = {    "httpProxy":PROXY,    "ftpProxy":PROXY,    "sslProxy":PROXY,    "noProxy":None,    "proxyType":"MANUAL",    "class":"org.openqa.selenium.Proxy",    "autodetect":False}# you have to use remote, otherwise you'll have to code it yourself in python to driver = webdriver.Remote("http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.FIREFOX)

You can read more about it here.


My solution:

def my_proxy(PROXY_HOST,PROXY_PORT):        fp = webdriver.FirefoxProfile()        # Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5        print PROXY_PORT        print PROXY_HOST        fp.set_preference("network.proxy.type", 1)        fp.set_preference("network.proxy.http",PROXY_HOST)        fp.set_preference("network.proxy.http_port",int(PROXY_PORT))        fp.set_preference("general.useragent.override","whater_useragent")        fp.update_preferences()        return webdriver.Firefox(firefox_profile=fp)

Then call in your code:

my_proxy(PROXY_HOST,PROXY_PORT)

I had issues with this code because I was passing a string as a port #:

 PROXY_PORT="31280"

This is important:

int("31280")

You must pass an integer instead of a string or your firefox profile will not be set to a properly port and connection through proxy will not work.