How can I get Webdriver Session ID in Selenium?

SeleniumAutomation TestingTesting Tools

We can get the webdriver session id with Selenium webdriver using the SessionId class. A session id is a distinctive number that is given to the webdriver by the server.

This number is utilized by the webdriver to establish communication with the browser. The commands in our Selenium tests are directed to the browser with the help of this session id. The method getSessionId is used to obtain the webdriver session id.

Syntax

SessionId s = ((RemoteWebDriver) driver).getSessionId();

Example

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.RemoteWebDriver;
public class BrwSessionId{
   public static void main(String[] args) {
      //set chromedriver.exe file path
      System.setProperty("webdriver.chrome.driver",
         "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      driver.get("https://www.tutorialspoint.com/index.htm");
      //get webdriver session id
      SessionId s = ((RemoteWebDriver) driver).getSessionId();
      System.out.println("Session Id is: " + s);
      //browser close
      driver.quit();
   }
}

Output

raja
Published on 07-Apr-2021 08:28:02
Advertisements