如何使用Selenium webdriver与Java为Firefox设置代理? [英] How do I set a proxy for firefox using Selenium webdriver with Java?

查看:407
本文介绍了如何使用Selenium webdriver与Java为Firefox设置代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.setProperty(webdriver.gecko.driver,E:\\\\\\\\\\\\\\\\\\);
Proxy p = new Proxy();
p.setSocksProxy(83.209.94.87:35923);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY,p);
WebDriver driver = new FirefoxDriver(cap);
driver.get(https://www.google.com.au);

这段代码在main方法里面。当我运行这段代码的时候,firefox已经启动了,但是google的url没有被跟踪,代理也没有被设置为我在上面代码中指定的那个。如何解决这个问题?

  public static void main(String [] args)throws InterruptedException,IOException,UnsupportedEncodingException {
while(true){
System.setProperty(webdriver.gecko.driver,E:\\\\\\\\\\\\\\\\\\\\
WebDriver驱动程序;
String PROXY =83.209.94.87:35923;
// Bellow给定语法将使用DesiredCapabilities设置浏览器代理设置。
Proxy proxy = new Proxy();
proxy.setAutodetect(false);
proxy.setProxyType(Proxy.ProxyType.MANUAL);
proxy.setSocksProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY,proxy);
//启动浏览器驱动程序实例时使用功能。
driver = new FirefoxDriver(cap);`


解决方案

<因为一个bug现在不能使用Proxy对象。您应该使用下面的代码:

  FirefoxProfile profile = new FirefoxProfile(); 
profile.setPreference(network.proxy.type,1);
profile.setPreference(network.proxy.socks,83.209.94.87);
profile.setPreference(network.proxy.socks_port,35923);

FirefoxDriver driver = new FirefoxDriver(profile);
driver.get(https://www.ipinfo.io);

这个bug在 https://github.com/mozilla/geckodriver/issues/764 ,你会看到在下面的链接中,Marionette驱动程序做了什么?



https:/ /dxr.mozilla.org/mozilla-central/source/testing/marionette/session.js#155



所以上面的代码只是复制相同的

p>

System.setProperty("webdriver.gecko.driver", "E:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
    Proxy p = new Proxy();
    p.setSocksProxy("83.209.94.87:35923");
    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability(CapabilityType.PROXY, p);
    WebDriver driver = new FirefoxDriver(cap);
    driver.get("https://www.google.com.au");

This code is inside the main method. When I run this code, firefox is launched but the google url isn't followed and the proxy is not set to the one I specify in the code above. How can I fix this?

public static void main(String[] args) throws InterruptedException, IOException, UnsupportedEncodingException {
    while (true) {
    System.setProperty("webdriver.gecko.driver", "E:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
    WebDriver driver;
    String PROXY = "83.209.94.87:35923";
      //Bellow given syntaxes will set browser proxy settings using DesiredCapabilities.
      Proxy proxy = new Proxy();
      proxy.setAutodetect(false);
      proxy.setProxyType(Proxy.ProxyType.MANUAL);
      proxy.setSocksProxy(PROXY);
      DesiredCapabilities cap = new DesiredCapabilities();
      cap.setCapability(CapabilityType.PROXY, proxy);
      //Use Capabilities when launch browser driver Instance.
      driver = new FirefoxDriver(cap);`

解决方案

Because a bug you cannot use the Proxy object as of now. You should use the below code

    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("network.proxy.type", 1);
    profile.setPreference("network.proxy.socks", "83.209.94.87");
    profile.setPreference("network.proxy.socks_port", 35923);

    FirefoxDriver driver = new FirefoxDriver(profile);
    driver.get("https://www.ipinfo.io");

The bug is discussed on https://github.com/mozilla/geckodriver/issues/764 and you see what the Marionette driver do in background on below link

https://dxr.mozilla.org/mozilla-central/source/testing/marionette/session.js#155

So above code just replicates the same

这篇关于如何使用Selenium webdriver与Java为Firefox设置代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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