我在java代码中使用Selenium在Firefox中打开Web应用程序。但我遇到了Firefox配置文件的问题,因为当我运行代码时,Firefox窗口使用新配置文件打开,因此无法打开Web应用程序,因为代理设置不同(我应该将IP地址添加到Firefox中没有代理IP) 。我尝试从我的代码中获取默认配置文件,但没有任何改变。我也试图创建新的配置文件,但我不知道如何添加IP。 我更改了代码,因此我可以手动打开Firefox,然后Selenium在nee选项卡中打开应用程序,因此IP将在那里。但这也失败了,代码仍然打开了新的窗口。 如果有人能提供帮助,我将非常感激。
答案 0 :(得分:1)
我们可以使用代理值创建一个firefox配置文件,并使用该配置文件打开firefox实例。下面的代码可能会提供一些想法。
public static void main(String[] args)
{
// Create proxy class object
Proxy p=new Proxy();
// Set HTTP Port to 7777
p.setHttpProxy("localhost:7777");
// Create desired Capability object
DesiredCapabilities cap=new DesiredCapabilities();
// Pass proxy object p
cap.setCapability(CapabilityType.PROXY, p);
System.setProperty("webdriver.gecko.driver", "//PATH");
WebDriver driver=new FirefoxDriver(cap);
}
希望这会有所帮助。感谢。
答案 1 :(得分:0)
由于你必须使用GeckoDriver来使用最新的firefox,所以你可以使用它在firefox中为geckodriver设置代理。
String PROXY = "localhost";
int PORT = 8080;
JSONObject json = new JsonObject();
json.addProperty("proxyType", "MANUAL");
json.addProperty("httpProxy", PROXY);
json.addProperty("httpProxyPort", PORT);
json.addProperty("sslProxy", PROXY);
json.addProperty("sslProxyPort", PORT);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("proxy", json);
GeckoDriverService service =new GeckoDriverService.Builder(firefoxBinary)
.usingDriverExecutable(new File("path to geckodriver"))
.usingAnyFreePort()
.usingAnyFreePort()
.build();
service.start();
// GeckoDriver currently needs the Proxy set in RequiredCapabilities
driver = new FirefoxDriver(service, cap, cap);