Skip to content
Advertisement

How to get browser name using Selenium WebDriver with Java?

I have a test case and need to execute based on the browser name i.e. IE or Chrome. In this test case some part will depend on browser type.

How will I get the browser name in between the execution? Example if it is IE, I need to pass the data. If it is Chrome browser, I need to select the data.

Advertisement

Answer

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 🙂

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement