Opening the chrome driver and browser on a specific port, and connecting to already opened chrome browser.
--
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:
Add alt text
Connecting to an existing chrome browser from selenium:
close all chrome browsers and then, start the Chrome browser in debug mode by opening cmd and running chrome.exe with — remote-debugging-port argument
"<path>\chrome.exe" --remote-debugging-port=1559
Now open any other browser and navigate to localhost:1559 to check that the browser was indeed opened with that port
Now in selenium code use chrome experimental option to connect to this chrome option
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("debuggerAddress", "127.0.0.1:1557");
WebDriver browser=new ChromeDriver(options);
Running Chrome on specific debug port from selenium:
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: