在Firefox Geckodriver的python中为selenium设置代理

时间:2017-08-29 22:37:25

标签: python-2.7 selenium-webdriver proxy geckodriver firefox-profile

我的问题是关于在Firefox(Geckodriver v0.18.0-win64)的python(2.7)中使用selenium(3.4.3。)编码设置代理。 规格在 http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp 仅提供了一个java示例。

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()

我收到以下错误消息:

  

selenium.common.exceptions.WebDriverException:消息:无法加载   轮廓。可能的firefox版本不匹配。你必须使用GeckoDriver   而不是Firefox 48 +。

如果我评论有关代理的代码,我可以在私人模式下获取页面作为指定的配置文件。我认为是代理搞砸了。

2 个答案:

答案 0 :(得分:6)

Yaso的回答对我没有用,相反我用了这个

proxyString = "Ip:port"

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

答案 1 :(得分:4)

我花了好几个小时找到答案,我想分享一下。 简单的问题出在代理规范中。 最初代理和端口是一个字符串

PROXY = "94.56.171.137:8080"

答案应该将端口设为数字

PROXY = "94.56.171.137"
PORT = 8080

以下是代码的其余部分

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