Where we learn technology

Overview of WebDriver and WebElement Interface in Selenium

Overview of WebDriver and WebElement Interface in Selenium

 
1. Introduction 
 
In this article, we are going to explore these powerful interfaces WebDriver and WebElement.
 
2. WebDriver Hierarchy
 
Before proceeding any further, let’s have a look at WebDriver Hierarchy first:
 
 
 
 
Explanation:
  • WebDriver is an interface and extends SearchContext Interface, which declares two methods findElement and findElements Methods (FindElement/FindElements in above image).
  • WebDriver has many abstract methods like: get(String url), close(), quit(),getTitle(), getCurrentUrl etc.
  • RemoteWebDriver class which implements WebDriver Interface, this class implement all the methods in WebDriver including findElement and findElements.
  • ChromeDriver Class extends ChromiumDriver Class which further extends RemoteWebDriver class.
  • And finally, IEDriver, SafariDriver and FirefoxDriver classes directly extends RemoteWebDriver class.
  • Click here to view complete source code implementation and for selenium java APIs visit here.
 
 
Let’s understand with a simple example:
 
When we say WebDriver driver = new ChromeDriver();
 
We can access all methods using ChromeDriver instance.
 

Below is the image for the same :

 

 
  
3. WebElement Interface
 
Now, let’s explore WebElement Interface:
 
  •  As explained on Selenium Official Site a WebElement represents HTML element.
  • The interface also extends SearchContext, TakesScreenshot interfaces.
  • In general, whatever we see in the HTML page is a WebElement whether it’s a link, search-text, button, Drop-Down, Web-Table etc. So, we can say that every action on a Web-Page while automating your Web Application will have to go through WebElement interface.
 
4. Ways to Define WebElement
 
       1)With the help of WebDriver instance
 
WebElement element = driver.findElement(By.);
 
findElement() – Basically it is a  method which finds a first single matched WebElement on the web page and returns the WebElement.
 
And similarly findElements() – It  returns List of matching WebElements that can be used to perform further actions.
 
Below code snippet where driver.findElements returning the List of WebElement.
 

By is an abstract class by which we can extract below methods which also be called as locators:
 
 

 
       
 
       
    2) By returning WebElement WebElement element = driver.findElement(By) and with above locators we can locate the WebElement on      the web page
 
    3)Using By Locator By by = By.name(“userName”);
WebElement element = driver.findElement(by);
 
 
 
 
5. Operations on WebElement
 
Once we figured out the WebElement with the help of locators then we can perform any actions on it.
 
Let’s look at below example:
 
WebElement userNameObj = driver.findElement(By.name(“userName”));
 
In above line of code, we found the WebElement with locator by name with WebElement instance as userNameObj.
Post that we need to send some userName to the WebElement, for that we need to write code as below
userNameObj.sendKeys(“sva”);
With the help of sendKeys, I just simply passed the username.
 
Let’s spend some time to look which all the post operations we can perform on WebElement:
 
 
 
 
 
 
 
 
 
As shown above, we can easily perform all these operation click(), clear(), sendKeys(), getText() and getAttribute() etc.
 
Below is the code snippet for your reference:
 
 
 
 
 
 

package com.example.automation;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

/**

* @author Mandeep Kaur

* @Date 21 April,2020

*/

public class SeleniumConfig {

public static void main(String[] args) {

String path = System.getProperty(“user.dir”);

System.setProperty(“webdriver.chrome.silentOutput”, “true”);

System.setProperty(“webdriver.chrome.driver”, path + “/chromedriver”);

WebDriver driver = new ChromeDriver();

driver.get(“http://newtours.demoaut.com/”);

WebElement userNameObj = driver.findElement(By.name(“userName”));

userNameObj.sendKeys(“sva”);

WebElement userPasswordObj = driver.findElement(By.name(“password”));

userPasswordObj.sendKeys(“123”);

WebElement loginObj = driver.findElement(By.name(“login”));

loginObj.click();

// get all the elements tag name a

List<WebElement> allLinksObj = driver.findElements(By.tagName(“a”));

// get the total count of elements

int linkCounts = allLinksObj.size();

for (int i = 0; i < linkCounts; i++) {

System.out.println(allLinksObj.get(i).getText());

}

driver.close();

}

}

 
6. Conclusion
 
In this article, we learnt about WebDriver and WebElement Interface.
 
 
 
Cheers!

Naveen AutomationLabs

 
Blog Contributors:
Author:  Mandeep Kaur
Mandeep, having 5+ years of Testing experience in automation using Selenium (Java). Expertise in API and Performance testing using JMeter.
 
Reviewer: Naveen 
 
 

2 Comments

  1. arjun singh

    Hi Mandeep,

    Please mention that the WebDriver hierarchy you have shown belongs to Selenium 4 alpha version like for example in Selenium 4 – ChromeDriver(C) Extends ChromiumDriver(C) and implements RemoteWebDriver(C) implements the WebDriver interface.

    but in selenium 3 – there is no ChromiumDriver Class, ChromeDriver(C) directly extends the RemoteDriver

    So please correct it dont take it otherwise. thanks

    • naveen

      Hi Arjun,
      Thanks for your comment. We are considering latest version of Selenium only.

Leave a Reply

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