

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Example
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"); } }
Output
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.
Example
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); } }
Output
Browser Window
- Related Questions & Answers
- How to open browser window in incognito/private mode using python selenium webdriver?
- How to open a new window on a browser using Selenium WebDriver for python?
- How to open a link in new tab of chrome browser using Selenium WebDriver?
- How to open a browser window in full screen using Selenium WebDriver with C#?
- How to open a new tab using Selenium WebDriver?
- How to launch Edge browser with Selenium Webdriver?
- How to open a link in new tab using Selenium WebDriver?
- How to close child browser window in Selenium WebDriver using Java?
- How to maximize the browser window in Selenium WebDriver using C#?
- How does selenium webdriver upload files to the browser?
- Capturing browser logs with Selenium WebDriver using Java.
- Is there any way to load an extension in chrome browser using Selenium Webdriver?
- Using the Selenium WebDriver - Unable to launch chrome browser on Mac
- How to connect to Chromium Headless using Selenium?
- How to open a new tab using Selenium WebDriver and start the link?