1

Exception in thread "main"

org.openqa.selenium.WebDriverException: 
java.net.ConnectException: 
Failed to connect to localhost/0:0:0:0:0:0:0:1:44248

I am frequently getting this error. The script runs halfway and then the exception occurs. Is there a way to avoid this.

Also, can we assign a particular port explicitly to web-driver execution, if yes, how can we do it

WebDriverConfig function

public static WebDriver DriverConfigDetails() {
 File src = new File("src");
 File driverloc = new File(src, "chromedriver.exe");
 System.out.println("\nDriver Path: " + driverloc.getAbsolutePath() + "\n");
 System.setProperty("webdriver.chrome.driver", driverloc.getAbsolutePath());
 WebDriver driver = new ChromeDriver();
 driver.manage().window().maximize();
 driver.manage().deleteAllCookies();
 driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
 return driver;
}
4
  • Can you post your code here? Mar 6 '20 at 4:45
  • public static WebDriver DriverConfigDetails() { File src = new File("src"); File driverloc = new File(src, "chromedriver.exe"); System.out.println("\nDriver Path: " + driverloc.getAbsolutePath() + "\n"); System.setProperty("webdriver.chrome.driver", driverloc.getAbsolutePath()); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().deleteAllCookies(); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); return driver; } Mar 6 '20 at 5:28
  • Please provide your Main Class code where you are using this function Mar 6 '20 at 5:42
  • is there any way to specify the localhost port for selenium explicitly Mar 6 '20 at 5:54
3

Yes, you can assign a particular port explicitly to your web-driver execution.

If you know the port you want to connect to, then you can directly connect to it using selenium with below code.

System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");
chromeOptions options = new ChromeOptions();
options.setExperimentalOption("debuggerAddress", "127.0.0.1:<customPort>");
WebDriver driver = new ChromeDriver(options);

Also, there is one another option to launch chrome browser at some custom port with below code:

chrome.exe --remote-debugging-port=<customPort> --user-data-dir="C:\selenium\AutomationProfile"

and then use that port for your execution with selenium.

Note: Just make sure that the custom port you are trying to use for your execution is not being used by any other application.

Reference

4
  • Hi, this doesn't start webdriver on the specific port, this just allows connecting to a chrome instance remotely(or from script) through the specified port, if the chrome is opened in the debugging mode through that port.
    – PDHide
    Mar 13 '20 at 7:48
  • @PDHide Yes, 2nd part of the code does the same as you said. But the first part of code can open the chrome at the custom part and one can do their execution on that port.
    – JAINAM
    Mar 13 '20 at 8:00
  • first part doesn't work if chrome is not already opened with the second part. It throws error , saying cannot connect at 127.0.0.1:port
    – PDHide
    Mar 13 '20 at 8:01
  • @PDHide It's working well with me.
    – JAINAM
    Mar 13 '20 at 8:43
1

This may help you to assign a particular port explicitly to web-driver execution

System.setProperty("webdriver.chrome.driver", "your driver path");
chromeOptions options = new ChromeOptions();
options.setExperimentalOption("debuggerAddress", "yourcustomport");
WebDriver driver = new ChromeDriver(options);`

Custom port should be provided along with your hostname

yourcustomport: 127.0.0.1:2100

note: your chrome browser should be running in this port

1

Runnin chromedriver on a specific port:

You should use ChromeDriverService for starting chrome in a different port:

       import org.openqa.selenium.chrome.WebDriver;
       import org.openqa.selenium.chrome.ChromeDriverService;
       import org.openqa.selenium.chrome.ChromeOptions;
       import org.openqa.selenium.remote.RemoteWebDriver;        


        WebDriver browser = null;

        ChromeDriverService service = new ChromeDriverService.Builder()
        .usingDriverExecutable(new File("C:\\chromedriver.exe"))
        .usingPort(4444) 
        .build();

        service.start(); 

        browser = new RemoteWebDriver(service.getUrl(), new ChromeOptions());

Once your tests are done, make sure to close the browser and the service:

browser.quit()
service.stop()

Output:

enter image description here

Running chrome on a specific debug port:

    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--remote-debugging-port=1557");
    WebDriver browser = new ChromeDriver(options);
    options.setExperimentalOption("debuggerAddress", "127.0.0.1:1557");
    browser=new ChromeDriver(options);

output:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.