How to connect to an already open browser using Selenium Webdriver?

We can connect to an already open browser using Selenium webdriver. This can be done using the Capabilities and ChromeOptions classes. The Capabilities class obtains the browser capabilities with the help of the getCapabilities method.
This is generally used for debugging purposes when we have a large number of steps in a test and we do not want to repeat the same steps. First of all we shall launch the browser and enter some text in the below edit box.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.By;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class ConnectBrwSession{
   public static void main(String[] args)
   throws InterruptedException{
      System.setProperty("webdriver.chrome.driver",
         "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      //get browser capabilities in key value pairs
      Capabilities c = driver.getCapabilities();
      Map<String, Object> m = c.asMap();
      m.forEach((key, value) −> {
         System.out.println("Key is: " + key + " Value is: " + value);
      });
      //set implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      driver.get("https://www.tutorialspoint.com/about/about_careers.htm");
      //identify element
      WebElement l = driver.findElement(By.id("gsc−i−id1"));
      l.sendKeys("Selenium");
   }
}
We shall note the parameter {debuggerAddress=localhost:61861} obtained from the Console output to be added to the ChromeOptions object.
Browser window −
Now, let us connect to the same browser session and perform some operations to it. We should not use browser close or quit methods while connecting to an existing session.
Code Modifications done to connect to the same session.
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.By;
import java.util.Map;
import org.openqa.selenium.WebDriver;
import java.util.concurrent.TimeUnit;
public class ConnectBrwSession{
   public static void main(String[] args)
   throws InterruptedException{
      System.setProperty("webdriver.chrome.driver",
         "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      //object of ChromeOptions class
      ChromeOptions o = new ChromeOptions();
      //setting debuggerAddress value
      o.setExperimentalOption("debuggerAddress", "localhost:61861");
      //add options to browser capabilities
      Capabilities c = driver.getCapabilities(o);
      Map<String, Object> m = c.asMap();
      m.forEach((key, value) −> {
         System.out.println("Key is: " + key + " Value is: " + value);
      });
      //set implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //identify element
      WebElement l = driver.findElement(By.id("gsc−i−id1"));
      //remove existing data in edit box
      l.clear();
      l.sendKeys("Tutorialspoint");
      String s = l.getAttribute("value");
      System.out.println("Attribute value: " + s);
   }
}