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.
Nikhil says
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
Mukesh Otwani says
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.
jatin says
Hello Mukesh sir,
This do not works with chrome 117.0.5938.132 version please tell me the solution
Mukesh Otwani says
Hi Jatin,
What issue you are getting?
Parth Gupta says
Thanks ! Was looking into this for so long !
Mukesh Otwani says
Most welcome
Mohini says
I want to do in Internet explorer using selenium but I am not getting this plz help
Mukesh Otwani says
It works for Chrome, Edge
dhananjay says
how to do this on mac
Mukesh Otwani says
Hi Dhanajay, for MAC also same process.
Anna Javier says
Hi,
It helped me a lot.
Thanks for this,
Anna
Mukesh Otwani says
Hey Anna, happy to help. Cheers.
nat says
awesome !!
Mukesh Otwani says
Thanks Nat
Harish M A says
Cool 🙂
It worked
Mukesh Otwani says
Awesome Harish
Shyam Sundar says
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.
Mukesh Otwani says
You can only handle windows which is started via CDP as I have mentioned in the video.
sanjib sarkar says
Can you share the same for Mac environment in safari browser?
Mukesh Otwani says
MAC does not support this.
Akh says
Hi Mukesh
Can you please do this in selenium C# also.?
Mukesh Otwani says
Sure will try
Tarun says
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.
Mukesh Otwani says
Hi Tarun,
I’ll post content on this soon. Please stay tuned
Santosh says
Hi,
Can you plz share code for EDGE browser?
Thanks,
Santosh
Mukesh Otwani says
Hi Santosh, what issues you are facing in this?
Md Osman says
I also want same code
Mukesh Otwani says
You can copy from blog and use the same.
Prashant says
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. 🙂
Mukesh Otwani says
You’re welcome, Prashant 🙂
Prasad says
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.
Mukesh Otwani says
Hi Prasad,
I tried with Edge and Chrome and its working fine. IE and Firefox I am not sure.
Priya says
Hi Mukesh, It would be very helpful if you could kindly share the Edge code as well!
Mukesh Otwani says
Hi Priya,
I’ll post it soon 🙂
Rocky says
Could you tell me is there any way to do this in Firefox too…I mean to open script in same browser
Mukesh Otwani says
Hi Rocky,
I’ll post for Firefox soon. Please stay tuned
Joy says
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!
Mukesh Otwani says
Yes, this should work in a Mac environment too.
vidya says
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
Mukesh Otwani says
Hi Vidya,
Please check chromedriver version with respect to the chrome browser version
Arpit says
is it possile for firefox?
Mukesh Otwani says
Hi Arpit,
I’ll check this & update soon
Paul says
Hi! Any idea if it`s possible to do something similar in VBA? Thank you
Mukesh Otwani says
Sorry paul no idea on this.
Robert says
Also looking for this!!
Lajish says
This is a much awaited post. Thanks for sharing such knowledge…:)