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.
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.
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.
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.
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.
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 :)
In Python, you may access the driver.capabilities
dict like this
driver.capabilities['browserName']
https://groups.google.com/forum/#!topic/selenium-users/nbSujBSc6q8
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();
}
}
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With