How to get browser name using Selenium WebDriver with Java?

To retrieve the Browser Name , Browser Version and Platform Name you can use either of the following approaches:

  • Using the API directly:

    • Code Block:

      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.firefox.FirefoxDriver;
      import org.openqa.selenium.remote.RemoteWebDriver;
      
      public class browserCapabilitiesRetrieve {
      
          public static void main(String[] args) {
      
              // initial configuration
              System.out.println("Browser Name is : "+((RemoteWebDriver) driver).getCapabilities().getBrowserName().toLowerCase());
              System.out.println("Browser Version is : "+((RemoteWebDriver) driver).getCapabilities().getVersion().toString());
              System.out.println("Platform Name is : "+((RemoteWebDriver) driver).getCapabilities().getPlatform().toString());
              driver.quit();
          }
      }
      
  • Using the Capabilities object and getCapability() method:

    • Code Block:

      import org.openqa.selenium.Capabilities;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.firefox.FirefoxDriver;
      import org.openqa.selenium.remote.RemoteWebDriver;
      
      public class FirefoxBrowserCapabilitiesRetrieve_getCapability {
      
          public static void main(String[] args) {
      
              // initial configuration
              Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();
              System.out.println("Browser Name is : "+cap.getBrowserName());
              System.out.println("Browser version is : "+cap.getVersion());           
              System.out.println("Platform is : "+cap.getPlatform().toString());
              driver.quit();
          }
      }
      

In Python, you may access the driver.capabilities dict like this

driver.capabilities['browserName']

https://groups.google.com/forum/#!topic/selenium-users/nbSujBSc6q8


You can use below code to know browser name, version and OS details:-

    Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();
    String browserName = cap.getBrowserName().toLowerCase();
    System.out.println(browserName);
    String os = cap.getPlatform().toString();
    System.out.println(os);
    String v = cap.getVersion().toString();
    System.out.println(v);

packages you need to import

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

OR

   Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();

    String browserName = cap.getBrowserName();
    String browserVersion = (String)cap.getCapability("browserVersion");
    String osName = Platform.fromString((String)cap.getCapability("platformName")).name().toLowerCase();

    return browserName + browserVersion + "-" + osName;

Hope it will help you :)