Advance Selenium

How To Execute Selenium Scripts On Already Opened Browser

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

How To Execute Selenium Scripts On Already Opened Browser

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

How To Execute Selenium Scripts On Already Opened Browser

 

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.

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

38 thoughts on “How To Execute Selenium Scripts On Already Opened Browser

  1. dhananjay says:

    how to do this on mac

    1. Hi Dhanajay, for MAC also same process.

  2. Anna Javier says:

    Hi,

    It helped me a lot.

    Thanks for this,
    Anna

    1. Hey Anna, happy to help. Cheers.

  3. nat says:

    awesome !!

  4. Harish M A says:

    Cool 🙂
    It worked

  5. 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.

    1. You can only handle windows which is started via CDP as I have mentioned in the video.

  6. sanjib sarkar says:

    Can you share the same for Mac environment in safari browser?

    1. MAC does not support this.

  7. Akh says:

    Hi Mukesh
    Can you please do this in selenium C# also.?

  8. 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.

    1. Hi Tarun,

      I’ll post content on this soon. Please stay tuned

      1. Santosh says:

        Hi,
        Can you plz share code for EDGE browser?

        Thanks,
        Santosh

        1. Hi Santosh, what issues you are facing in this?

        2. Md Osman says:

          I also want same code

          1. You can copy from blog and use the same.

  9. 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. 🙂

    1. You’re welcome, Prashant 🙂

  10. 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.

    1. Hi Prasad,

      I tried with Edge and Chrome and its working fine. IE and Firefox I am not sure.

      1. Priya says:

        Hi Mukesh, It would be very helpful if you could kindly share the Edge code as well!

        1. Hi Priya,

          I’ll post it soon 🙂

          1. Rocky says:

            Could you tell me is there any way to do this in Firefox too…I mean to open script in same browser

          2. Hi Rocky,

            I’ll post for Firefox soon. Please stay tuned

  11. 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!

    1. Yes, this should work in a Mac environment too.

  12. 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

    1. Hi Vidya,

      Please check chromedriver version with respect to the chrome browser version

  13. Arpit says:

    is it possile for firefox?

    1. Hi Arpit,

      I’ll check this & update soon

  14. Paul says:

    Hi! Any idea if it`s possible to do something similar in VBA? Thank you

    1. Sorry paul no idea on this.

    2. Robert says:

      Also looking for this!!

  15. Lajish says:

    This is a much awaited post. Thanks for sharing such knowledge…:)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.