Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 446
Swathi Chowdary Avatar asked Feb 07 '16 19:02

Swathi Chowdary


People also ask

How do I find the URL of a webpage using Selenium?

To get the the current URL of web page programmatically using Selenium in Java, initialize a web driver and call getCurrentUrl() method on the web driver object. WebDriver. getCurrentUrl() method returns a string representing the current URL that the browser is looking at.

How do you get the title in Selenium?

We can obtain the page title using Selenium webdriver. The method getTitle() is used to obtain the present page title and then we can get the result in the console.

How do I find the Windows name in Selenium?

Get the handle of the parent window using the command: String parentWindowHandle = driver. getWindowHandle(); Print the window handle of the parent window. Find the element on the web page using an ID which is an element locator.

How does Webdriver talk to the browser?

as explained in the above answer browser webdriver interacts with the real browser as a HTTP Request. Every Browser Driver uses an HTTP server to receive HTTP requests. Once the URL reaches the Browser Driver, then it will pass that request to the real browser over HTTP.


4 Answers

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 :)

like image 165
Shubham Jain Avatar answered Oct 17 '22 13:10

Shubham Jain


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

driver.capabilities['browserName']

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

like image 31
user7610 Avatar answered Oct 17 '22 12:10

user7610


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();
          }
      }
      
like image 2
undetected Selenium Avatar answered Oct 17 '22 13:10

undetected Selenium


For those using C# you can do the following to detect browser when using either the local browser driver or remotewebdriver:

        public static bool IsSafari(IWebDriver driver)
        {
            // Using remotewebdriver e.g. browserstack
            if (SomeConfig.UsingRemoteWebDriver)
                return GetRemoteDriverBrowserName(driver) == "safari";
            // Using local browser driver
            return driver.GetType() == typeof(SafariDriver);
        }

        public static bool IsInternetExplorer(IWebDriver driver)
        {
            // Using remotewebdriver e.g. browserstack
            if (SomeConfig.UsingRemoteWebDriver)
                return GetRemoteDriverBrowserName(driver) == "internet explorer";
            // Using local browser driver
            return driver.GetType() == typeof(InternetExplorerDriver);
        }

        private static string GetRemoteDriverBrowserName(IWebDriver driver)
        {
            return ((RemoteWebDriver)driver).Capabilities.GetCapability("browserName").ToString().ToLower();
        }
like image 1
Rushby Avatar answered Oct 17 '22 12:10

Rushby