selenium – 通过木偶驱动程序在Firefox浏览器中设置代理

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

但我在firefox-profile中的设置(代理必须设置为network.proxy.type = 4,自动检测)不再应用于Firefox配置(Firefox打开但默认设置了所有设置)并且我的测试在没有正确的情况下无法正常工作PROXY配置.

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

最佳答案 使用firefoxProfile或Proxy类的旧技巧不再起作用.您所要做的就是使用新的Marionette代理格式传递requiredCapabilities和
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;

import 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

点赞