Python + selenium + firefox 代理不起作用

标签 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')

有人可以帮助我吗?

谢谢!

最佳答案

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

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)

它对我来说是成功的。
需要注意的是:不要将noProxy设置为None'',将其删除。
看我博客更详细的说明:http://www.itfanr.cc/

关于Python + selenium + firefox 代理不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42335857/

相关文章:

javascript - Python Selenium 屏蔽字段

java - 当 VM 最小化时,Selenium 无法单击弹出窗口。

javascript - jQuery 虚拟键盘插件在 Windows 8 触摸设备上插入相同字符两次

javascript - 相同的选择器在常规浏览器和 Selenium 中给出不同的结果

css - 如何创建 CSS 浏览器特定边框

css - GWT ListBox 在不同浏览器中呈现不同

python - 如何在不使用多线程的情况下显示进度

python - 冲突的 OpenCV 和 Matplotlib

python - Selenium -无法找到youtube搜索栏

python - 如何在按钮上添加单独的 Html 元素单击 Python Dash