17

How can I redirect the traffic of Firefox launched by Selenium in Python to a proxy? I have used the solutions suggested on the web but they don't work!

I have tried:

profile = webdriver.FirefoxProfile() 
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", "54.213.66.208")
profile.set_preference("network.proxy.http_port", 80)
profile.update_preferences() 
driver = webdriver.Firefox(profile)
Ratmir Asanov
  • 5,889
  • 5
  • 24
  • 38
Michele Spina
  • 981
  • 4
  • 10
  • 20
  • There might be more than one solution on the web. What did you try? (In particular, did you try [this](http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#firefox)?) – unutbu Sep 10 '13 at 13:21
  • I have tried with this one: profile = webdriver.FirefoxProfile() profile.set_preference("network.proxy.type", 1) profile.set_preference("network.proxy.http", "54.213.66.208") profile.set_preference("network.proxy.http_port", 80) profile.update_preferences() driver = webdriver.Firefox(profile) – Michele Spina Sep 10 '13 at 13:31
  • Is your URL using `http:` or `https:`? – unutbu Sep 10 '13 at 13:44
  • 1
    http. I lunch this command driver.get("http://whatismyipaddress.com") and the ip is not the ip of the proxy but by ip... – Michele Spina Sep 10 '13 at 13:58
  • did you find out how to make this work? I have the same issue and I am using Firefox46 – salvob Feb 14 '17 at 22:57

3 Answers3

21

You need to import the following:

from selenium.webdriver.common.proxy import Proxy, ProxyType

Then setup the proxies:

myProxy = "xx.xx.xx.xx:xxxx"

proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': myProxy,
    'ftpProxy': myProxy,
    'sslProxy': myProxy,
    'noProxy': '' # set this value as desired
    })

Then call the webdriver.Firefox() function as follows:

driver = webdriver.Firefox(proxy=proxy)
driver.get("http://www.google.com")

Don't remember where exactly I found this solution, but its out there somewhere. Will surely provide a link if I find it again. Just took this part out of my code.

Ratmir Asanov
  • 5,889
  • 5
  • 24
  • 38
amitdatta
  • 710
  • 6
  • 14
  • I think you get that code from the JAVA standalone-server, wich uses this way to found desired browser capabilities. Is really this working? – m3nda Jun 27 '15 at 07:22
  • this is the solution for python, not java. This definitely works for me. – amitdatta Jul 06 '15 at 22:11
  • My mistake. The way you wrote that function is very similar to the Java example guides, when the python guides do not offer all the possible ways to code something, while supporting. Maybe you are a expert coder, o maybe you simply learned this way, but is not present on the noob guides. I have look a way to do that using profiles with name, but didn't was aware about howto edit the current one, the random created profile, the current instance. I will test that for sure. Thank you. – m3nda Jul 08 '15 at 20:15
  • 3
    Important to note: `myProxy` has to be `IPAddress:port` or `hostname:port` where hostname doesn't contain the _"http://"_ prefix – salvob Feb 16 '17 at 15:43
  • How about changing the proxy later? – Jawad Jul 25 '21 at 01:30
  • 1
    What does `noProxy` mean? What is an example of a value for it? – André C. Andersen Oct 03 '21 at 10:14
15

Try this for firefox/geckodriver:

proxy = "212.66.117.168:41258"

firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True

firefox_capabilities['proxy'] = {
    "proxyType": "MANUAL",
    "httpProxy": proxy,
    "ftpProxy": proxy,
    "sslProxy": proxy
}

driver = webdriver.Firefox(capabilities=firefox_capabilities)

And for Chrome you can use:

proxy = "212.66.117.168:41258"
prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = proxy
prox.socks_proxy = proxy
prox.ssl_proxy = proxy

capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities(capabilities)
driver = webdriver.Chrome(desired_capabilities=capabilities)
OceanFire
  • 464
  • 4
  • 9
  • Thanks. If you want you can also use my python library to get free and fresh proxy https://github.com/OceanFireSoftware/python-simple-proxy – OceanFire Aug 02 '19 at 11:12
  • This worked for me!!!! So many of the solutions for this are missing the `firefox_capabilities['marionette'] = True`. Thank you for sharing! – Clay Sep 06 '19 at 22:53
  • This is useful. After so many trial and errors this is the only solution worked in Latest Firefox/Geckodriver setup. As after 46 version of firefox you need to use geckodriver and for that you can set up proxy this way. – TapanHP Sep 24 '19 at 13:48
  • thx. The only solution that actually works. Should be marked as "Accepted answer" – PATAPOsha Sep 28 '21 at 13:53
  • worked without `['marionette'] = True` for me.. I guess since it is True by default =) – PATAPOsha Sep 28 '21 at 13:55
6

Your problem is on the driver init. Try webdriver = webdriver.Firefox(firefox_profile=profile) All the other code is OK and you can also remove profile.update_preferences() line.

I found YOUR solution with a 2 minute google search. How much time did you spend waiting for? :D

Your problem is that you maybe read code from other langs but Python. Replace this webdriver.Firefox(profile) with webdriver.Firefox(firefox_profile=profile).

Your code should be:

profile = webdriver.FirefoxProfile() 
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", "54.213.66.208")
profile.set_preference("network.proxy.http_port", 80)
profile.update_preferences() 
driver = webdriver.Firefox(firefox_profile=profile)
m3nda
  • 1,832
  • 3
  • 32
  • 44
  • No. `__init__` method of WebDriver (imported as Firefox) accepts `firefox_profile` as first argument. – Raz Jul 27 '15 at 10:40
  • Have to admit that if you use the named variable is not same as first "positional", but we are talking different things at all. This will be similar to run Firefox("firefox", profile) without named arguments. – m3nda Jun 15 '17 at 12:43
  • 1
    I'm not sure what are you talking about. Check the [source](https://github.com/SeleniumHQ/selenium/blob/master/py/selenium/webdriver/firefox/webdriver.py#L55). `Firefox(firefox_profile=profile)` is equal to `Firefox(profile)`. – Raz Jun 15 '17 at 20:49