4

Firefox 47 及更高版本不支持 Selenium Webdriver。我尝试使用 Marionette 驱动程序通过 Firefox 开始我的测试。

但是我在 firefox-profile 中的设置(代理必须设置为network.proxy.type = 4,自动检测)不再适用于 Firefox 配置(Firefox 打开,但所有设置默认设置),如果没有正确的代理配置,我的测试将无法工作。

如何通过 Marionette 驱动程序在 Firefox 浏览器中设置代理?

4

1 回答 1

1

firefoxProfile 或 Proxy 类的旧技巧不再起作用。您所要做的就是通过 requiredCapabilities 使用新的 Marionette 代理格式传递一个 JSON:

[{proxy={"proxyType":"MANUAL","httpProxy":"host.proxy","httpProxyPort":80,"sslProxy":"host.proxy","sslProxyPort":80}}]

不再有“proxyHost:proxyPort”格式,而是 httpProxy=host, httpProxyPort=port。

JsonObject json = new JsonObject();
json.addProperty("proxyType", "MANUAL");
json.addProperty("httpProxy", aHost);
json.addProperty("httpProxyPort", aPort);
json.addProperty("sslProxy", aHost);
json.addProperty("sslProxyPort", aPort);

required.setCapability("proxy", json);

driver = new FirefoxDriver(service, cap, required);

这是所有代码:

 import java.io.File;

导入 java.io.IOException;

import org.apache.commons.lang.SystemUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.GeckoDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonObject;

public class TestProxy
{
  public static void main(String[] args)
  { 
    if (args.length == 0)
    {
      System.out.println("usage: IP port");
      System.exit(-1);
    }

    String proxyServer = args[0];
    int proxyPort = Integer.parseInt(args[1]);

    try
    {
      TestProxy test = new TestProxy();
      test.TestDriver(proxyServer, proxyPort);
    } catch (IOException e)
    {
      e.printStackTrace();
    }    
  }

  private void TestDriver(String aHost, int aPort) throws IOException
  {
    WebDriver driver = null;
    GeckoDriverService service = null;

    if (SystemUtils.IS_OS_LINUX)
    {
      FirefoxBinary firefoxBinary = new FirefoxBinary(new File("/home/ubuntu/firefox/firefox"));

      service = new GeckoDriverService.Builder(firefoxBinary)
          .usingDriverExecutable(new File("/usr/bin/geckodriver"))
          .usingAnyFreePort()
          .usingAnyFreePort()
          .withEnvironment(ImmutableMap.of("DISPLAY",":20"))
          .build();
      service.start();      
    }
    else if (SystemUtils.IS_OS_WINDOWS)
    {
      FirefoxBinary firefoxBinary = new FirefoxBinary(new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe"));

      service = new GeckoDriverService.Builder(firefoxBinary)
          .usingDriverExecutable(new File("C:\\Windows\\geckodriver.exe"))
          .usingAnyFreePort()
          .usingAnyFreePort()
          .build();
      service.start();      
    }
    else
    {
      System.out.println("Unknown operation system");
      System.exit(-1);
    }

    DesiredCapabilities cap = DesiredCapabilities.firefox();
    DesiredCapabilities required = new DesiredCapabilities();

    JsonObject json = new JsonObject();
    json.addProperty("proxyType", "MANUAL");
    json.addProperty("httpProxy", aHost);
    json.addProperty("httpProxyPort", aPort);
    json.addProperty("sslProxy", aHost);
    json.addProperty("sslProxyPort", aPort);

    required.setCapability("proxy", json);

    driver = new FirefoxDriver(service, cap, required);

    driver.navigate().to("https://api.ipify.org/?format=text");

    System.out.println(driver.getPageSource());

    driver.quit();
  }
}

https://github.com/SeleniumHQ/selenium/issues/2963

https://github.com/mozilla/geckodriver/issues/97#issuecomment-255386464

于 2016-10-21T14:15:14.923 回答