• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / How To Execute Selenium Scripts On Already Opened Browser

How To Execute Selenium Scripts On Already Opened Browser

February 24, 2021 by Mukesh Otwani 46 Comments

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.

Filed Under: Advance Selenium

Reader Interactions

Comments

  1. Nikhil says

    October 10, 2023 at 9:10 AM

    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

    Reply
    • Mukesh Otwani says

      October 31, 2023 at 12:29 PM

      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.

      Reply
  2. jatin says

    October 4, 2023 at 1:10 PM

    Hello Mukesh sir,
    This do not works with chrome 117.0.5938.132 version please tell me the solution

    Reply
    • Mukesh Otwani says

      October 9, 2023 at 1:51 PM

      Hi Jatin,

      What issue you are getting?

      Reply
  3. Parth Gupta says

    September 29, 2023 at 7:20 PM

    Thanks ! Was looking into this for so long !

    Reply
    • Mukesh Otwani says

      October 9, 2023 at 1:51 PM

      Most welcome

      Reply
  4. Mohini says

    September 28, 2023 at 6:38 PM

    I want to do in Internet explorer using selenium but I am not getting this plz help

    Reply
    • Mukesh Otwani says

      October 9, 2023 at 1:51 PM

      It works for Chrome, Edge

      Reply
  5. dhananjay says

    July 14, 2023 at 12:25 AM

    how to do this on mac

    Reply
    • Mukesh Otwani says

      July 24, 2023 at 12:22 AM

      Hi Dhanajay, for MAC also same process.

      Reply
  6. Anna Javier says

    March 5, 2023 at 3:21 PM

    Hi,

    It helped me a lot.

    Thanks for this,
    Anna

    Reply
    • Mukesh Otwani says

      March 18, 2023 at 1:03 AM

      Hey Anna, happy to help. Cheers.

      Reply
  7. nat says

    February 22, 2023 at 8:59 AM

    awesome !!

    Reply
    • Mukesh Otwani says

      July 24, 2023 at 1:06 AM

      Thanks Nat

      Reply
  8. Harish M A says

    January 10, 2023 at 7:48 PM

    Cool 🙂
    It worked

    Reply
    • Mukesh Otwani says

      January 24, 2023 at 10:55 AM

      Awesome Harish

      Reply
  9. Shyam Sundar says

    January 10, 2023 at 1:23 AM

    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.

    Reply
    • Mukesh Otwani says

      January 24, 2023 at 10:56 AM

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

      Reply
  10. sanjib sarkar says

    September 21, 2022 at 2:42 PM

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

    Reply
    • Mukesh Otwani says

      October 8, 2022 at 1:14 PM

      MAC does not support this.

      Reply
  11. Akh says

    June 22, 2022 at 5:40 PM

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

    Reply
    • Mukesh Otwani says

      July 22, 2022 at 1:11 PM

      Sure will try

      Reply
  12. Tarun says

    May 16, 2022 at 10:33 PM

    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.

    Reply
    • Mukesh Otwani says

      May 19, 2022 at 1:26 AM

      Hi Tarun,

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

      Reply
      • Santosh says

        November 29, 2022 at 6:52 PM

        Hi,
        Can you plz share code for EDGE browser?

        Thanks,
        Santosh

        Reply
        • Mukesh Otwani says

          December 31, 2022 at 12:53 PM

          Hi Santosh, what issues you are facing in this?

          Reply
        • Md Osman says

          July 11, 2023 at 7:09 PM

          I also want same code

          Reply
          • Mukesh Otwani says

            July 24, 2023 at 12:21 AM

            You can copy from blog and use the same.

  13. Prashant says

    April 4, 2022 at 8:32 PM

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

    Reply
    • Mukesh Otwani says

      April 9, 2022 at 10:11 AM

      You’re welcome, Prashant 🙂

      Reply
  14. Prasad says

    February 19, 2022 at 12:34 PM

    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.

    Reply
    • Mukesh Otwani says

      February 21, 2022 at 11:26 AM

      Hi Prasad,

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

      Reply
      • Priya says

        March 21, 2022 at 7:23 PM

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

        Reply
        • Mukesh Otwani says

          March 21, 2022 at 10:39 PM

          Hi Priya,

          I’ll post it soon 🙂

          Reply
          • Rocky says

            May 6, 2022 at 6:39 PM

            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

            May 19, 2022 at 1:27 AM

            Hi Rocky,

            I’ll post for Firefox soon. Please stay tuned

  15. Joy says

    December 17, 2021 at 10:56 AM

    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!

    Reply
    • Mukesh Otwani says

      December 25, 2021 at 11:28 AM

      Yes, this should work in a Mac environment too.

      Reply
  16. vidya says

    November 26, 2021 at 12:03 PM

    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

    Reply
    • Mukesh Otwani says

      December 2, 2021 at 10:23 AM

      Hi Vidya,

      Please check chromedriver version with respect to the chrome browser version

      Reply
  17. Arpit says

    August 11, 2021 at 11:55 AM

    is it possile for firefox?

    Reply
    • Mukesh Otwani says

      August 23, 2021 at 11:04 AM

      Hi Arpit,

      I’ll check this & update soon

      Reply
  18. Paul says

    August 3, 2021 at 10:56 PM

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

    Reply
    • Mukesh Otwani says

      August 4, 2021 at 12:35 PM

      Sorry paul no idea on this.

      Reply
    • Robert says

      January 17, 2022 at 11:12 PM

      Also looking for this!!

      Reply
  19. Lajish says

    February 26, 2021 at 9:51 PM

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

    Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?