gpt4 book ai didi

selenium - 通过 marionette 驱动程序在 Firefox 浏览器中设置代理

转载 作者:行者123 更新时间:2023-12-01 04:55:14 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

关于selenium - 通过 marionette 驱动程序在 Firefox 浏览器中设置代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37774403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com