setting proxy in selenium in python for Firefox Geckodriver

Deadly 提交于 2019-12-06 07:58:41

问题


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.


回答1:


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
        }



回答2:


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


来源:https://stackoverflow.com/questions/45949274/setting-proxy-in-selenium-in-python-for-firefox-geckodriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!