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");


这段代码在main方法内部。当我运行此代码时,将启动firefox,但不会遵循google url,并且代理未设置为我在上面的代码中指定的代理。我怎样才能解决这个问题?

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);`

最佳答案

由于存在错误,您目前无法使用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");


该错误在https://github.com/mozilla/geckodriver/issues/764上进行了讨论,您可以在下面的链接中看到Marionette驱动程序在后台执行的操作

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

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

关于java - 如何使用带有Java的Selenium Webdriver设置Firefox的代理?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45810476/

10-09 00:28