Selenium WebDriver With Examples

We need browsers to test web applications. Selenium automates browser and helps us in automation of web application testing across different browsers. Selenium API has provided many classes and interfaces to work with different types of browsers and HTML elements.

What is Selenium WebDriver Interface?

Selenium WebDriver is an interface that defines a set of methods. However, implementation is provided by the browser specific classes. Some of the implementation classes are AndroidDriver, ChromeDriver, FirefoxDriver, InternetExplorerDriver, IPhoneDriver, SafariDriver etc.

The WebDriver main functionality is to control the browser. It even helps us to select the HTML page elements and perform operations on them such as click, filling a form fields etc.

If we want to execute your test cases in a Firefox browser we have to use FirefoxDriver class. Similarly, if we want to execute the test cases in the Chrome browser we have to use ChromeDriver class.

Selenium WebDriver Methods

SearchContext is the topmost interface in Selenium API which has two methods – findElement() and findElements().

Selenium WebDriver interface has many abstract methods like get(String url), quit(), close(), getWindowHandle(), getWindowHandles(), getTitle() etc.

WebDriver has nested interfaces like Window, Navigation, Timeouts etc. These nested interfaces are used to perform operations like back(), forward() etc.

Method Description
get(String url) This method will launch a new browser and opens the given URL in the browser instance.
getWindowHandle() It is used to handle single window i.e. main window. It return type is string. It will returns browser windlw handle from focused browser.
getWindowHandles() It is used to handle multiple windows. It return type is Set. It will returns all handles from all opened browsers by Selenium WebDriver.
close() This command is used to close the current browser window which is currently in focus.
quit() This method will closes all the browsers windows which are currently opened and terminates the WebDriver session.
getTitle() This method is used to retrieve the title of the webpage the user currently working on.

List of classes implementing WebDriver

The major implementation classes of WebDriver interface are ChromeDriver, EdgeDriver, FirefoxDriver, InternetExplorerDriver etc. Each driver class corresponds to a browser. We simply create the object of the driver classes and work with them.

Class Description
ChromeDriver It helps you to execute Selenium Scripts on Chrome browser.
FirefoxDriver It helps you to execute Selenium Scripts on Firefox browser.
InternetExplorerDriver It helps you to execute Selenium Scripts on InternetExplorer browser.

List of Commands on WebElement

Selenium WebElement represents an HTML element. We can get an instance of WebElement using findElement() method and then perform specific actions such as click, submit etc.

Some of the commonly used WebElement methods are:

Command Description Syntax
findElement() This method finds the first element within the current web page by using given locator. WebElement element = driverObject.findElement(By.locator(“value”));
sendKeys() This method enters a value in to an Edit Box or Text box. driver.findElement(By.elementLocator(“value”)).sendkeys(“value”);
clear() It clears the Value from an Edit box or Text Box. driverObject.findElement(By.locatorname(“value”)).clear();
click() It clicks an Element (Button, Link, Checkbox) etc. driverObject.findElement(By.ElementLocator(“LocatorValue”)).click();

Selenium WebDriver Example – Print Website Title

Let’s look at a simple example of using Selenium WebDriver to invoke Firefox browser and print the title of a website.



Selenium WebDriver Example

Reference: WebDriver GitHub Code

By admin

Leave a Reply