setting proxy in selenium in python for Firefox Geckodriver

8.7k views Asked by At

My question is about setting proxy in selenium (3.4.3.) coding in python (2.7) for Firefox (Geckodriver v0.18.0-win64). The spec at http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp provides only a java example.

from selenium import webdriver
PROXY = "94.56.171.137:8080"
class Proxy(object):        
    def __call__(self):    
        self.base_url = "https://whatismyip.com"
        print self.base_url        
        # proxy json object
        desired_capability = webdriver.DesiredCapabilities.FIREFOX['proxy']={
           "httpProxy":PROXY,
            "ftpProxy":PROXY,
            "sslProxy":PROXY,
            #"noProxy":None,
            "proxyType":"manual"
        }    
        firefox_profile = webdriver.FirefoxProfile()
        firefox_profile.set_preference("browser.privatebrowsing.autostart", True)
        self.driver = webdriver.Firefox(executable_path='D:\Code\Drivers\geckodriver',firefox_profile=firefox_profile,  capabilities=desired_capability)         
        self.driver.get(self.base_url)    

if __name__ == "__main__":        
    proxy_test = Proxy()
    proxy_test()

I am getting the following Error Message:

selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Possible firefox version mismatch. You must use GeckoDriver instead for Firefox 48+.

If I comment the code regarding the proxy, I am able to get the page, in private mode as the profile specified. I think it is the proxy that is messing things up.

2

There are 2 answers

0
Yaso On

I spent hours finding an answer and I want to share it. The simple problem was in the proxy specification. Initially the proxy and port were one string

PROXY = "94.56.171.137:8080"

the answer should make the port as a number

PROXY = "94.56.171.137"
PORT = 8080

Here is the rest of the code

from selenium import webdriver

PROXY = "94.56.171.137"
PORT = 8080

class Proxy(object):        
    def __call__(self):    
        self.base_url = "https://whatismyip.com"
        print self.base_url        
        # https://github.com/mozilla/geckodriver
        # proxy json object
        desired_capability = webdriver.DesiredCapabilities.FIREFOX
        desired_capability['proxy']={
            "proxyType":"manual",
            "httpProxy":PROXY,
            "httpProxyPort": PORT,
            "ftpProxy":PROXY,
            "ftpProxyPort": PORT,
            "sslProxy":PROXY,
            "sslProxyPort" : PORT
        }        

        firefox_profile = webdriver.FirefoxProfile()
        firefox_profile.set_preference("browser.privatebrowsing.autostart", True)
        self.driver = webdriver.Firefox(executable_path='D:\Drivers\geckodriver',firefox_profile=firefox_profile,  capabilities=desired_capability) 

        self.driver.get(self.base_url)    

if __name__ == "__main__":    

    proxy_test = Proxy()
    proxy_test()    code here
0
Tintinabulator Zea On

Yaso's answer didn't work for me, instead i used this

proxyString = "Ip:port"

desired_capability = webdriver.DesiredCapabilities.FIREFOX
        desired_capability['proxy'] = {
            "proxyType": "manual",
            "httpProxy": proxyString,
            "ftpProxy": proxyString,
            "sslProxy": proxyString
        }