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.
- What is the code that you are using and what is the error. Please add these in the problem statement.demouser123– demouser1232017-06-06 16:02:37 +00:00Commented Jun 6, 2017 at 16:02
- You need to open firefox with a profile which handles the proxysanthosh kumar– santhosh kumar2017-06-06 16:41:21 +00:00Commented Jun 6, 2017 at 16:41
- @L.M.O Can you consider showing your work please? Thanksundetected Selenium– undetected Selenium2017-06-06 17:53:03 +00:00Commented Jun 6, 2017 at 17:53
Add a comment |
2 Answers
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.
2 Comments
L.M.O
Can you please explain what does port 7777 refer to? And where can I set the server IP address?
santhosh kumar
Local host is the server IP address. If there is no proxy then leave it. So it will look like p.setHttpProxy("172.17.95.1");
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);