![](https://i0.wp.com/learn-automation.com/wp-content/uploads/2021/02/How-To-Execute-Selenium-Scripts-On-Already-Opened-Browser-1.png?resize=750%2C420&ssl=1)
There can be many scenarios where you need to run a Selenium test or script on an already opened browser for debugging purposes.
If you want to start the Chrome browser in Selenium without any debugging mode then here is the detailed post and video on the same.
To start the selenium test on the existing or already opened window we need to use Chrome DevTools Protocol.
What is Chrome DevTools Protocol
Ans- Chrome DevTools Protocol formally known as CDP it enables us to communicate with chrome browser and Chromium.
It can help us with debugging, tracing, troubleshooting, test automation, and many more.
You can find more information about CDP using this link.
https://chromedevtools.github.io/devtools-protocol/
How To Execute Selenium Scripts On Already Opened Browser
Step 1- Start Chrome in debug mode
Navigate to chrome directory using the cd command
cd C:\Program Files (x86)\Google\Chrome\Application
In my case chrome.exe is under C:\Program Files (x86)\Google\Chrome\Application
Syntax
chrome.exe –remote-debugging-port=any-free-port –user-data-dir=directory path where you need to store data
Example
chrome.exe –remote-debugging-port=9222 –user-data-dir=E:\chromeData
Type above command and hit enter
Once you see chrome is running fine (on the port) then you can use the same browser window for your selenium test as well.
Step 2- Execute Selenium test on port 9222
// set the driver path- You can also use WebDriverManager for drivers System.setProperty("webdriver.chrome.driver","E:\\MukeshData\\chromedriver.exe"); // Create object of ChromeOptions Class ChromeOptions opt=new ChromeOptions(); // pass the debuggerAddress and pass the port along with host. Since I am running test on local so using localhost opt.setExperimentalOption("debuggerAddress","localhost:9222 "); // pass ChromeOptions object to ChromeDriver constructor WebDriver driver=new ChromeDriver(opt); // now you can use now existing Browser driver.get("http://facebook.com");
If you are wondering that how do I get that I need to pass debuggerAddress in the setExperimentalOption method right?
Let me show you how you can get the same information using the program so next time you don’t need to remember this option.
Program 2- Get debugger port using the program
// set the driver path- You can also use WebDriverManager for drivers System.setProperty("webdriver.chrome.driver","E:\\MukeshData\\chromedriver.exe"); // Create object of ChromeDriver class ChromeDriver driver=new ChromeDriver(); // getCapabilities will return all browser capabilities Capabilities cap=driver.getCapabilities(); // asMap method will return all capability in MAP Map<String, Object> myCap=cap.asMap(); // print the map data- System.out.println(myCap);
The output of the above program
Once you get the session with debugging port then you can use the same port for execution.
I hope this post would help you to run the Selenium test on the existing browser. If you find any issues then feel free to connect.
Thank you and will see you in the next video.
How to do it on CDD machine as I am unable to access Chrome application path –
Do I need to take help of administrator to run the command in cdd machine
Hi Nikhil, if you have admin privilege then you can do this because we need to run this as app. Note – Please use Selenium version 3 for this.
Hello Mukesh sir,
This do not works with chrome 117.0.5938.132 version please tell me the solution
Hi Jatin,
What issue you are getting?
Thanks ! Was looking into this for so long !
Most welcome
I want to do in Internet explorer using selenium but I am not getting this plz help
It works for Chrome, Edge
how to do this on mac
Hi Dhanajay, for MAC also same process.
Hi,
It helped me a lot.
Thanks for this,
Anna
Hey Anna, happy to help. Cheers.
awesome !!
Thanks Nat
Cool 🙂
It worked
Awesome Harish
Hi Mukesh,
i tried the solution and its opening in the new window and not able to use the existing window. Not sure where i going wrong here.
You can only handle windows which is started via CDP as I have mentioned in the video.
Can you share the same for Mac environment in safari browser?
MAC does not support this.
Hi Mukesh
Can you please do this in selenium C# also.?
Sure will try
Hi Mukesh,
You mentioned that this worked with Edge browser too. I tried but I am unable to make it work on Edge. Can you please share your code for the Edge browser.
Thank You.
Hi Tarun,
I’ll post content on this soon. Please stay tuned
Hi,
Can you plz share code for EDGE browser?
Thanks,
Santosh
Hi Santosh, what issues you are facing in this?
I also want same code
You can copy from blog and use the same.
Thank you Mukesh.
I was in a situation wherein I had to come up with a workaround for logging in to the system. Since there was more like a two-factor authentication involved. This solution allowed me to just log in once, and continue on the opened page.
PS: One thing to notice is that I think the driver.quit() gets overridden too which was cool.
Thanks for your help. 🙂
You’re welcome, Prashant 🙂
Hi Mukesh,
Is it possible in IE Browser?
Because I have a scenario like desktop application opens payment gateway window in IE Browser by default so, I want to handle this using selenium to get it done easier and go back to desktop application.
Hi Prasad,
I tried with Edge and Chrome and its working fine. IE and Firefox I am not sure.
Hi Mukesh, It would be very helpful if you could kindly share the Edge code as well!
Hi Priya,
I’ll post it soon 🙂
Could you tell me is there any way to do this in Firefox too…I mean to open script in same browser
Hi Rocky,
I’ll post for Firefox soon. Please stay tuned
Hi Mukesh good day!
Is it possible to also have the same step by step on how to do this using Mac? I am using Eclipse, Java. Thanks!
Yes, this should work in a Mac environment too.
It was working fine for me earlier..
But now I am not able run my scripts on already opened browser. It says
org.openqa.selenium.WebDriverException: unknown error: cannot connect to chrome at localhost:9222
from chrome not reachable.
I am trying to connect to chrome version 96
Kindly help
Hi Vidya,
Please check chromedriver version with respect to the chrome browser version
is it possile for firefox?
Hi Arpit,
I’ll check this & update soon
Hi! Any idea if it`s possible to do something similar in VBA? Thank you
Sorry paul no idea on this.
Also looking for this!!
This is a much awaited post. Thanks for sharing such knowledge…:)