Opening the chrome driver and browser on a specific port, and connecting to already opened chrome browser.
2 min readMar 13, 2020
Introduction:
This article helps you in understanding how to start chrome driver on a specific port, connect to an existing browser session and opening chrome browser with debug port opened through selenium scripts.
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()
Else the port remains open.
Output: