Selenium WebDriver

Filed Under: Selenium

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.

Selenium WebDriver

Selenium WebDriver

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.

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.

ChromeDriverIt helps you to execute Selenium Scripts on Chrome browser.
FirefoxDriverIt helps you to execute Selenium Scripts on Firefox browser.
InternetExplorerDriverIt 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:

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.

package com.journaldev.selenium.firefox;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GeckoDriverExample {

	public static void main(String[] args) {
		//specify the location of GeckoDriver for Firefox browser automation
		System.setProperty("webdriver.gecko.driver", "geckodriver");
		WebDriver driver = new FirefoxDriver();
		String PageTitle = driver.getTitle();
		System.out.println("Page Title is:" + PageTitle);


1551941763563	mozrunner::runner	INFO	Running command: "/Applications/" "-marionette" "-foreground" "-no-remote" "-profile" "/var/folders/1t/sx2jbcl534z88byy78_36ykr0000gn/T/rust_mozprofile.t6ZyMHsrf2bh"
1551941764296	WARN	Loading extension '': Reading manifest: Invalid host permission: resource://pdf.js/
1551941764297	WARN	Loading extension '': Reading manifest: Invalid host permission: about:reader*
Can't find symbol 'GetGraphicsResetStatus'.
1551941765794	Marionette	INFO	Listening on port 61417
1551941765818	Marionette	WARN	TLS certificate errors will be ignored for this session
Mar 07, 2019 12:26:05 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Page Title is:JournalDev - Java, Java EE, Android, Python, Web Development Tutorials
1551941814652	Marionette	INFO	Stopped listening on port 61417
Selenium WebDriver Example

Selenium WebDriver Example

You can checkout more Selenium examples from our GitHub Repository.

Reference: WebDriver GitHub Code

Leave a Reply

Your email address will not be published. Required fields are marked *

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages