0

Every time I restart my Java application. Then create a new instance of the ChromeDriver() class. This in turn starts a new Chrome browser.

I want to be able to carry on where I left off. So while I'm building code for a given step or webpage. One of many in my automation sequence. I can edit code and relaunch. Without having to start the whole thing or sequence again.

This is my basic Java implementation to start a chrome browser which is ready for automation.

    package jack;
    import org.openqa.selenium.*;
    import org.openqa.selenium.chrome.*;
    
    public class JacksClass {
    
           WebDriver driver;
           
           public void launchBrowser() {
                  
                  // Set file path of chrome driver
                  System.setProperty("webdriver.chrome.driver",  "C:\\chromedriver.exe");
                  
                  // Create object
                  driver = new ChromeDriver();
                  
                  // Open our browser to this URL
                  driver.get("https://google.com");
                  
           }
           
           public static void main(String[] args) {
    
                  JacksClass obj = new JacksClass();
                  
                  obj.launchBrowser();
           
           }
    }

Is this possible by saving a session ID? Then reusing that session ID next time on startup? So I can just reconnect to the current browser open?

Can an amazing individual edit my code to do what I need? Thank you, I appreciate the community.

Jack Trowbridge
  • 3,175
  • 9
  • 32
  • 56

1 Answers1

1

If you're using Chrome, there is a possibility to attach new session to an opened browser window. What you have to do is described here:

https://medium.com/@harith.sankalpa/connect-selenium-driver-to-an-existing-chrome-browser-instance-41435b67affd

Basically:

  1. run Chrome with params chrome --remote-debugging-port=
  2. add ChromeOptions param debuggerAddress
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("debuggerAddress","localhost:<remote-port>");
ChromeDriver driver = new ChromeDriver(options);
``` (source - https://medium.com/@harith.sankalpa/connect-selenium-driver-to-an-existing-chrome-browser-instance-41435b67affd)
Piotr M.
  • 385
  • 2
  • 8