Sometimes we come across scenarios where we want Selenium to connect to and use an existing browser that was previously opened manually or by some other program.
Why do we want Selenium to connect to a previously opened browser?
- Maybe you want to perform some tasks that require manual intervention before kicking in automation execution. For instance, we may need to manually launch the browser, navigate to the desired page, perform some manual tasks like entering captcha manually and then run the automation script to continue from there.
- Clicking a button on a desktop application opens a webpage. We have some other tool for automating the desktop application but we want Selenium to take control the moment the webpage is opened.
- Debugging – say we are working on a test script for a certain screen (product checkout) in the application. But the script to reach to that page (add to cart) is not ready yet. We can manually launch the browser and navigate to that page and then run the automation script.
Now, we are back to the original question – How to connect Selenium to an existing browser session?
We can take advantage of the Chrome DevTools Protocol. It allows clients to inspect and debug the Chrome browser.
Prerequisite: Add Chrome to PATH
Please make sure the path to chrome’s executable is added to the environment variable PATH
. You can check it by running the command chrome.exe
(on Windows) or Google/ Chrome
( on Mac). It should launch the Chrome browser.
If you get a similar message as below that means Chrome is not added to your system’s path:
'chrome' is not recognized as an internal or external command,
operable program or batch file.
If this is the case, please feel free to Google how to add chrome to PATH? If you are on Mac, please follow the steps in This Link.
Step 1: Launch browser with custom flags
To enable Chrome to open a port for remote debugging, we need to launch it with a custom flag –
chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\ChromeProfile"
For Mac:
Google\ Chrome --remote-debugging-port=9222 --user-data-dir="~/ChromeProfile"
For --remote-debugging-port
value you can specify any port that is open.
For --user-data-dir
flag you need to pass a directory where a new Chrome profile will be created. It is there just to make sure chrome launches in a separate profile and doesn’t pollute your default profile.
Once Chrome is launched this way it has opened a connection on the given port that any client can connect to for debugging.
You can now play with the browser manually, navigate to as many pages, perform actions and once you need your automation code to take charge, you may run your automation script. You just need to modify your Selenium script to make Selenium connect to that opened browser.
You can verify if the Chrome is launched in the right way:
- Launch a new browser window normally (without any flag), and navigate to
http://127.0.0.1:9222
- Confirm if you see the Google homepage reference in the second browser window
Step 2: Launch browser with options
Here is that simple but magical Java and Python code. You can easily convert it into a Programming language of your choice.
// JAVA Example
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
//Change chrome driver path accordingly
System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");
WebDriver driver = new ChromeDriver(options);
System.out.println(driver.getTitle());
# PYTHON Example
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
#Change chrome driver path accordingly
chrome_driver = "C:\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options)
print driver.title
What is that value under debuggerAddress?
The URL 127.0.0.1
denotes your localhost. We have supplied the same port, 9222
that we used to launch Chrome with --remote-debugging-port
flag. While you can use any port, you need to make sure it is open and available to use.
Troubleshooting
I cannot connect to the browser window.
- Confirm, if you have launched the browser the same way as described in the tutorial – Check this section
- Manually go to
www.google.com
in the same browser window - Launch a new browser window normally (without any flag), and navigate to
http://127.0.0.1:9222
- Confirm if you see the Google homepage reference in the second browser window
I can connect to the browser window manually but not through the code.
- Verify you have imported the right packages. Refer the code block above.
- Check if you have right chrome driver as per your installed Chrome version. Please refer This Link
If you have found this tutorial useful, please share it with the world.