Webdriver and WebElement Interface in selenium web driver

Webdriver and WebElement Interface in selenium web driver

What is Webdriver and WebElement Interface in selenium web driver? In this selenium web driver tutorial. I will try to cover all the details about Webdriver and WebElement Interface.

Web driver is an interface which contains lots of methods which helps to automate a web application. web driver in selenium provide supports of multiple browsers like chrome, firefox, IE. RemoteWebDriver class implements the WebDriver interface which is extended by browser drivers.

What is web driver in selenium

To understand web driver in selenium you need to understand a scenario

These days multiple browsers available in the market like Chrome, Firefox, Opera, Safari etc. And every browser has its own way to implements things like lunching browser, close browser minimizes the browser and so on.

It’s not possible for selenium developer to develop the separate framework for every browser. And browser team also not allowed to change their source code according to selenium requirement.

So they create an agent that is called browser classes Like Chrome driver, Firefox driver. These driver classes work as an interface between selenium and browsers or you can say its communication medium between selenium and browsers.

Selenium developers do not know logic and logic of all the browser so they created WebDriver as an interface. which contain all the abstract methods without logic and implementation and gave to browsers to handle them whenever these methods are called on browsers.

RemoteWebDriver class implements the WebDriver interface which is extended by browser drivers class.

So now If I want to use perform an operation with chrome browser I just need to create an object of Chrome driver.

WebDriver driver = new ChromeDriver();

Now this driver object can access all the elements of Chrome browser.

Web driver is an Interface which contain all the abstract methods.

 

What is WebElement Interface in selenium web driver

In this tutorial we are going to see, WebElement Interface in selenium web driver. WebElement is a representation of HTML elements. HTML elements are the tag which is start and end with some tag. For example heading tag: <h1>Contain….</h1> here h1 is tag or we can say HTML elements.

An HTML element can contain multiple elements that are called nested elements.

<!DOCTYPE html>
<html>
<head>
    <title>Demo tittle</title>
</head>
<body>

    <h1>This is heading one</h1>

    <input type="text" name="username" id="name" placeholder="enter your name..">

</body>
</html> 

To handle this elements selenium provides WebElement Interface. There are lots of useful methods in the WebElement interface to interact with this HTML elements and for performing operations.

NOTE:

WebElement interface is implemented by RemoteWebElement, AndroidWebElement, or HtmlUnitWebElement.

Methods of WebElement Interface.

WebElement Interface in selenium web driver

How to get element using WebElement Interface in selenium web driver

To get elements from the HTML page WebDriver provides a method is called findelement(By arg0) and this will return an element of WebElement type.

WebElement element = driver.findElement(By.xpath(""));

Now we have an element in this element object now we need to perform an action on this element so we will use below methods.

Followings are some important methods which are commonly used.

click():void

This method is used for only click on the elements it will not return anything.

For example, we have a button element and we just need to click on that button.

WebElement button = driver.findElement(By.xpath("//*[@id=\"btn_basic_example\"]"));
    
button.click();

clear():void

This method uses to clear any input box and it will not return any value.

For example, we want to clear text from an input box.

WebElement input = driver.findElement(By.xpath("//*[@id=\"user-message\"]"));
input.clear();

sendKeys(CharSequence… keysToSend ) : void

This method uses to set a text value to an input box and it takes a string a parameter.

For example, If we want to send any text in an input box.

WebElement input = driver.findElement(By.xpath("//*[@id=\"user-message\"]"));
        input.sendKeys("Hello Folks");

getText():String

This method will return a text of the element.

For example: Take the text of the heading.

WebElement input = driver.findElement(By.xpath("//*[@id=\"easycont\"]/div/div[2]/h3"));
       String headingText = input.getText();

isEnabled():boolean

This method used to verify the element is to enable or disable for example I want to check a button is enable or disable.

WebElement button = driver.findElement(By.xpath("//*[@id=\"btn_basic_example\"]"));
    button.isEnabled();

isDisplayed():boolean

This method is used to verify the element is displayed or not.

For Example:

WebElement button = driver.findElement(By.xpath("//*[@id=\"btn_basic_example\"]"));
    button.isDisplayed();

isSelected():boolean

This method is used to verify an element is select or not. It is widely used in checkboxes, radio boxes, and options in select.

Add a Comment

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