Firefox 47及更高版本不支持Selenium Webdriver。我尝试使用Marionette驱动程序通过Firefox开始我的测试。
但是我在firefox-profile中的设置(代理必须设置为network.proxy.type = 4
,自动检测)不再适用于Firefox配置(Firefox打开但默认情况下设置了所有设置)并且我的测试不起作用正确的PROXY配置。
如何通过Marionette驱动程序在Firefox浏览器中设置代理?
答案 0 :(得分:1)
使用firefoxProfile或Proxy类的旧技巧不再起作用。您所要做的就是使用新的Marionette代理格式传递requiredCapabilities和JSON:
[{代理= { “的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