Tutorial

Selenium WebDriver

Updated on March 21, 2024
author

Meghna Gangwar

Selenium WebDriver

What is a Webdriver

Webdriver is a tool used in web application development to automate testing across multiple types of browsers. Since browsers such as Chrome, Safari, Firefox and Edge all have a tendency to display web apps and websites differently, developers need a efficient way to test accurately at scale. Selenium is a popular webdriver which automates the browser and helps us in automating 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. Kinda like a remote control. 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.

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.

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();
		driver.get("https://journaldev.com");
		String PageTitle = driver.getTitle();
		System.out.println("Page Title is:" + PageTitle);
		driver.close();
	}
}

Output:

1551941763563	mozrunner::runner	INFO	Running command: "/Applications/Firefox.app/Contents/MacOS/firefox-bin" "-marionette" "-foreground" "-no-remote" "-profile" "/var/folders/1t/sx2jbcl534z88byy78_36ykr0000gn/T/rust_mozprofile.t6ZyMHsrf2bh"
1551941764296	addons.webextension.screenshots@mozilla.org	WARN	Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid host permission: resource://pdf.js/
1551941764297	addons.webextension.screenshots@mozilla.org	WARN	Loading extension 'screenshots@mozilla.org': 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

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Meghna Gangwar

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.