- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can Selenium interact with an existing browser session?
We can interact with an existing browser session. This is performed by 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 ConnectExistingSession{ 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 existing session.
import org.openqa.selenium.WebDriver; 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 java.util.concurrent.TimeUnit; public class ConnectExistingSession{ 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 Articles
- How does selenium interact with the Web browser?
- How to close a browser session in Selenium with python?
- How to interact with hidden elements in Selenium Webdriver?
- Browser Plugin Testing With Selenium.
- Use Selenium with Chromium Browser.
- How can I get Webdriver Session ID in Selenium?
- Does HTML5 allow you to interact with local client files from within a browser?
- Can Selenium use multi threading in one browser?
- How to start selenium browser with proxy?
- Does HTML5 allow you to interact with local client files from within a web browser?
- Capturing browser logs with Selenium WebDriver using Java.
- Clear browser Cookies with Selenium WebDriver Java bindings.
- How to launch Edge browser with Selenium Webdriver?
- Python selenium browser driver.back().
- Selenium testing without browser.
