3

我的问题是关于在 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 版本不匹配。对于 Firefox 48+,您必须使用 GeckoDriver。

如果我评论有关代理的代码,我可以按照指定的配置文件以私有模式获取页面。我认为是代理把事情搞砸了。

4

2 回答 2

6

Yaso的回答对我不起作用,而是我用了这个

proxyString = "Ip:port"

desired_capability = webdriver.DesiredCapabilities.FIREFOX
        desired_capability['proxy'] = {
            "proxyType": "manual",
            "httpProxy": proxyString,
            "ftpProxy": proxyString,
            "sslProxy": proxyString
        }
于 2018-01-08T02:52:50.917 回答
5

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

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
于 2017-09-08T17:09:01.310 回答