Do this method to connect the already running web browser to speed up Selenium automation

M Shahid Siddiqui
3 min readNov 15, 2020

--

When people start web browser automation using Selenium, they often follow the basic tutorials and just learn more about elements and locators. It leaves a huge gap in the process. For each test case development, they have to relaunch the browser, which eats up most of the time than really executing the business logic.

Solution: Don’t launch the browser when not needed. Instead, just connect to the running instance.

— — — — — — — — — — — — — — — — — — —

Check out this page for more interesting topics

Unique Source of Knowledge

— — — — — — — — — — — — — — — — — — —

How?

Before explaining how I want to highlight the assumptions or a checklist of preparation.

Assumptions

  1. The Browser under test is Chrome web browser (installed)

2. Selenium Webdriver is configured along with Java (same logic should work with Python or any other language. Important is to know the Chrome flags)

3. You have sufficient rights to manage the PC (just launching command.exe)

4. I am using Windows 10. But I’m assuming that you can translate the executable path on your machine (Mac or Linux, or your custom paths on Windows)

5. I will use port 9229 to connect from the java code. I manually created a folder (you can create some other path)

C:\Users\ShahidSiddiqui\Chrome

Now, here are the steps:

  1. Open command prompt (command.exe) with your account (admin access is not needed) [Start > cmd.exe and press Enter]
  2. Paste the below command and press ENTER key:
"C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9229 --user-data-dir="C:\Users\ShahidSiddiqui\Chrome"

3. It will launch a fresh instance of Chrome (even if chrome is running, it will not interfere so don’t be worried).

4. Now, build & execute the below java code:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Isolated {

public static void main(String[] args){
System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Binaries\\Webdriver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-debugging-port=9229");
options.setExperimentalOption("debuggerAddress", "127.0.0.1:9229");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.facebook.com");
System.out.println(driver.getTitle());
}
}

Here is the screenshot of the code in the editor:

Isolated.java
Save the file as Isolated.java

Above screenshot is added to guide you properly.

Now, just run the above java code.

Dynamically it will load the page(s) as specified in the code without interrupting the other Chrome sessions.

Reach me if you want to debug or discuss more.

Follow me if you found this article useful. It will help others to understand that this page is good for them and it will be helpful to them as well as others.

All the best with automated browsing.

--

--

M Shahid Siddiqui
M Shahid Siddiqui

Written by M Shahid Siddiqui

Tech Savy | IT Enthusiast | Served 14 Years in IT | M.B.M from University of Auckland, New Zealand

No responses yet