1
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.proxy import *

myProxy = "user:pass@proxy:port"

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

driver = webdriver.Firefox(proxy=proxy)
driver.set_window_size(1024, 768)
driver.get('http://whois.urih.com/')

When directed to that website it still shows my local proxy. I got the above from the selenium site but it has not availed me.

EDIT: I just tried changing the format to proxy:port:user:pass and this time the proxy seems engaged since now the dialog box opened to input username and pass. Well this is no good either. Surely I don't need to try the roundabout way of focusing the alert and inputting the info there...

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
RoryGillum
  • 93
  • 1
  • 10
  • I might be wrong, but can it be that httpProxy, ftpProxy, etc supposed to be a host, while username and password should be in socksUsername and socksPassword? – timbre timbre Nov 30 '15 at 22:19
  • @KirilS. You are right, I checked in firefox network configurations and I did not find anything related to proxy user name and password – Omar Mowafi Dec 01 '15 at 15:42

1 Answers1

1

This is one of the working ways to setup proxy for firefox webdriver

from selenium import webdriver

firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("network.proxy.type", 1)
firefox_profile.set_preference("network.proxy.http",ip) #set your ip
firefox_profile.set_preference("network.proxy.http_port", port) #set your port
driver = webdriver.Firefox(firefox_profile=firefox_profile)
driver.get(url) #target url
Omar Mowafi
  • 854
  • 5
  • 13
  • While I agree that this is one of the ways to get proxy properly configured for Firefox, calling it a "correct way to setup the proxy for selenium" is incorrect, while the method the OP is using IS correct. For instance using this method you need to have proxy configuration for each browser (IE, Chrome will have different ways to set the proxy on browser level), and potentially you may end up with more branches vor various versions of the browser, if some version changes their internal implementation of proxy... – timbre timbre Nov 30 '15 at 22:29
  • How am I supposed to set it up if my proxy fromat is: ip:port:user:pass? It isn't clear. Or alternative formet: user:pass@ip:port – RoryGillum Nov 30 '15 at 22:31
  • @KirilS. I agree but I have tried many different ways and this was the only one that reflected on firefox – Omar Mowafi Nov 30 '15 at 22:34
  • @RoryGillum Check this link http://stackoverflow.com/questions/17082425/running-selenium-webdriver-with-a-proxy-in-python – Omar Mowafi Nov 30 '15 at 22:37
  • Where are username and password supposed to go? That other thread doesn't offer any new information over what is posted above excepting changing port to int. – RoryGillum Dec 01 '15 at 11:47
  • @RoryGillum I can't find a way to input the user name and password. if firefox prompts you. Using selenium why don't you insert username then press tab and insert password – Omar Mowafi Dec 01 '15 at 15:57
  • I tried; didn't work. See: http://stackoverflow.com/questions/34021474/python-why-isnt-this-selenium-alert-send-keys-working – RoryGillum Dec 01 '15 at 16:24