Reputation: 119
I'm using Selenium in java code to open a web application in Firefox. But I faced a problem with Firefox profile because when I run the code, Firefox window openned using new profile, so the web application couldn't be opened since the proxy settings are different (I should add the IP address into Firefox no proxy IPs). I tried get the default profile from my code, but nothing changed. I also tried to create new profile but I don't know how to add the IP to it. I changed the code so I can open the Firefox manually and then Selenium opens the application in nee tab, so the IP will be there. But this also failed and the code still opens new window. I'll be very thankful if anyone can help.
Upvotes: 1
Views: 495
Reputation: 1086
Since you have to use GeckoDriver to use latest firefox so you can setup proxy in firefox for geckodriver using this.
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);
Upvotes: 0
Reputation: 2019
We can create a firefox profile with proxy value and open the firefox instance with that profile. Below code might give some idea.
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);
}
Hope this helps. Thanks.
Upvotes: 1