2

我的問題是關於在python(2.7)for Firefox(Geckodriver v0.18.0-win64)中爲selenium(3.4.3。)編碼設置代理。 規格 http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp 只提供了一個java示例。在python for Firefox中爲硒設置代理Geckodriver

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+。

如果我評論有關代理的代碼,我能夠以隱私模式獲取頁面,並以指定的配置文件。我認爲這是代理亂了事。

+0

我認爲這個錯誤是相當自我解釋的。你需要更新Firefox。 – Kapil

+0

不,我有最新的Firefox版本55.0.3。 – Yaso

回答

2

我花了數小時找到答案,我想分享它。 簡單的問題出現在代理規範中。 最初的代理和端口是一個字符串

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 
3

YASO的回答沒」的其餘部分t爲我工作,而我用這個

proxyString = "Ip:port" 

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