【问题标题】:using http proxy with selenium Geckodriver使用带有 selenium Geckodriver 的 http 代理
【发布时间】:2022-01-22 07:41:54
【问题描述】:

我尝试了一些方法,但都没有奏效。任何人都有使用 Geckodriver for Selenium 3 的 HTTP 代理的工作示例?我正在使用 Java 绑定

这是我尝试过的

        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        Proxy proxy = new Proxy();
        proxy.setHttpProxy("proxyip:proxyport");
        capabilities.setCapability("proxy", proxy);

        System.setProperty("webdriver.gecko.driver", "C:\\geckodriver-v0.16.1-win64\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver(capabilities);

【问题讨论】:

  • 你能给我们看看你的一些作品吗?
  • 可能是因为你 DesiredCapabilities.chrome() 但接下来你在 system.property 中使用了 geckodriver.exe
  • @Dev 我用 firefox 试过了,我仍然得到一个异常 pastebin.com/YSt3626w
  • 好的,在错误堆栈跟踪中我可以看到SessionNotCreatedException 异常,这意味着甚至没有调用木偶。 Firefox 是否通过您的机器上的驱动程序打开?
  • yes firefox 打开但不加载页面。如果我不传递它的功能,那么它也会加载页面并且我可以与之交互。

标签: selenium selenium-webdriver geckodriver


【解决方案1】:

要使用 Firefox 浏览器启用 代理,您需要创建一个新配置文件并将其传递给驱动程序,如下所示:

  • 设置HTTP代理:

    FirefoxProfile profile = new FirefoxProfile();         
    profile.setPreference("network.proxy.type", 1);
    profile.setPreference("network.proxy.http", "localhost");     
    profile.setPreference("network.proxy.http_port", 3128); 
    WebDriver driver = new FirefoxDriver(profile);
    
  • 设置SSL代理:

    FirefoxProfile profile = new FirefoxProfile();         
    profile.setPreference("network.proxy.type", 1);
    profile.setPreference("network.proxy.ssl", "localhost");     
    profile.setPreference("network.proxy.ssl_port", 3128); 
    WebDriver driver = new FirefoxDriver(profile);
    

【讨论】:

  • profile.setPreference("network.proxy.ssl", "localhost"); profile.setPreference("network.proxy.ssl_port", 3128);
  • 这对我今天最新的 Firefox 和 Geckodriver 不起作用,但官方文档规定的方法有效:selenium.dev/documentation/en/webdriver/http_proxies
  • @Motin,那些文档只描述了 Chrome。你是如何让它与 Firefox 一起工作的?
【解决方案2】:

如果您需要使用授权代理,那么 selenium 将无法做到这一点。由于 selenium 不支持处理要求输入密码的警报。我为 Firefox 编写了一个附加组件,您可以使用它解决代理问题以及 cookie 和标头问题。

https://github.com/alexsok-bit/selenium_helper

您可以使用发布目录中的插件,它已经签名

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 2016-12-05
    • 2021-09-05
    • 1970-01-01
    相关资源
    最近更新 更多