2017-02-20 88 views
0

我單獨運行Firefox,設置代理,可以正常工作。我通過硒運行Firefox,設置代理,但它不起作用!代理服務器是一樣的!Python +硒+ Firefox代理不起作用

代碼1:

from selenium import webdriver 
    from selenium.webdriver.common.proxy import * 

    use_proxy=agent_IP+':'+str(agent_Port) 

    _proxy = Proxy({ 
     'proxyType': ProxyType.MANUAL, 
     'httpProxy': use_proxy, 
     'ftpProxy': use_proxy, 
     'sslProxy': use_proxy, 
     'noProxy': None, # set this value as desired 
     "proxyType":"MANUAL", 
     "class":"org.openqa.selenium.Proxy", 
     "autodetect":False 
     }) 
    browser = webdriver.Firefox(proxy=_proxy) 
    browser.get('https://www.google.com') 

代碼2:

from selenium import webdriver 

    profile = webdriver.FirefoxProfile() 
    # Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5 
    profile.set_preference("network.proxy.type", 1) 
    profile.set_preference("network.proxy.share_proxy_settings", True) 
    profile.set_preference("network.http.use-cache", False) 
    profile.set_preference("network.proxy.http", agent_IP) 
    profile.set_preference("network.proxy.http_port", int(agent_Port)) 
    profile.set_preference('network.proxy.ssl_port', int(agent_Port)) 
    profile.set_preference('network.proxy.ssl', agent_IP) 
    profile.set_preference("general.useragent.override","whater_useragent") 
    profile.update_preferences() 
    browser = webdriver.Firefox(firefox_profile=profile) 
    browser.get('https://www.google.com') 

有人能幫助我嗎?

THX!

+0

通過硒設置代理後,我發現在firefox裏面設置的代理是正確的,就像手動設置一樣! –

+1

你有沒有想過這個想法? – cph

回答

0

我也遇到了同樣的問題,這兩種方法我都試過了,確實都是無效的。但後來找到了一條可行的辦法。

firefox_capabilities = DesiredCapabilities.FIREFOX 
firefox_capabilities['marionette'] = True 

PROXY = "58.216.202.149:8118" 

firefox_capabilities['proxy'] = { 
    "proxyType": "MANUAL", 
    "httpProxy": PROXY, 
    "ftpProxy": PROXY, 
    "sslProxy": PROXY 
} 

driver = webdriver.Firefox(capabilities=firefox_capabilities) 
driver.get(url) 

這對我來說是成功的。
需要注意的是:不要將noProxy設置爲None'',請將其刪除。
看看我的博客的更詳細的描述:http://www.itfanr.cc/