Python + selenium + firefox代理不起作用

时间:2017-02-20 03:10:23

标签: python selenium firefox proxy

我单独运行Firefox,设置代理,可以正常工作。我通过selenium运行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!

2 个答案:

答案 0 :(得分:6)

我也遇到了同样的问题,我试过的两种方法确实都是无效的。但后来找到了一种可行的方法。

firefox_capabilities = webdriver.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)

它为我带来了成功。
请务必注意:不要使用noProxyNone设置'',请将其删除。
查看我的博客的更详细说明:http://www.itfanr.cc/

答案 1 :(得分:0)

只需补充一下,我使用以下配置即可进行设置,

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary


profile = webdriver.FirefoxProfile()

user_agent = "Some UserAgent String here..."
proxy_ip = "123.123.123.123"
proxy_port = "12345"

profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", str(proxy_ip))
profile.set_preference("network.proxy.http_port", int(proxt_port))
profile.set_preference("network.proxy.ssl", str(proxy_ip))
profile.set_preference("network.proxy.ssl_port", int(proxt_port))
profile.set_preference("network.proxy.ftp", str(proxy_ip))
profile.set_preference("network.proxy.ftp_port", int(proxt_port))
profile.set_preference("network.proxy.socks", str(proxy_ip))
profile.set_preference("network.proxy.socks_port", int(proxt_port))
profile.set_preference("network.http.use-cache", False)

profile.set_preference("general.useragent.override", user_agent)

profile.update_preferences()
binary = FirefoxBinary("/usr/bin/firefox")
driver = webdriver.Firefox(firefox_profile=profile, firefox_binary=binary)

driver.get("https://ifconfig.me")

driver.save_screenshot("check_ip.png")
driver.quit()

因此只需将HTTP代理设置为所有其他协议即可使其正常工作。 希望这对某人有帮助。