为Firefox Geckodriver在python中设置硒中的代理 [英] setting proxy in selenium in python for Firefox Geckodriver

查看:164
本文介绍了为Firefox Geckodriver在python中设置硒中的代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于在Firefox(Geckodriver v0.18.0-win64)的python(2.7)中设置硒(3.4.3.)编码中的代理. 的规格 http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp 仅提供一个Java示例.

My question is about setting proxy in selenium (3.4.3.) coding in python (2.7) for Firefox (Geckodriver v0.18.0-win64). The spec at http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp provides only a java example.

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

selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Possible firefox version mismatch. You must use GeckoDriver instead for Firefox 48+.

如果我注释了有关代理的代码,则可以在私有模式下按指定的配置文件获取页面.我认为是代理弄乱了事情.

If I comment the code regarding the proxy, I am able to get the page, in private mode as the profile specified. I think it is the proxy that is messing things up.

推荐答案

Yaso的答案不适用于我,而是我使用了此

Yaso's answer didn't work for me, instead i used this

proxyString = "Ip:port"

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

这篇关于为Firefox Geckodriver在python中设置硒中的代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆