What is WebElement?


The elements that are present on a web page are called WebElement. It represents an HTML element. 

A web page consists of many different types of HTML elements such as text boxes, links, buttons, dropdown buttons, radio buttons, labels, and forms. These are called WebElements. 

An HTML element is made up of a start tag and end tag, with the content inserted in between. It has the following format:
           <tagname> Content </tagname>
where, 
            <tagname> ➨ Start tag.
            </tagname> ➨ End tag.

Let's analyze the following example of WebElement.
1. <label> Enter your username </label>
Here, <label> is start tag of WebElement label. The text "Enter your username" is the content which is present on the label element. </label> is the end tag that represents end of WebElement.

2. Similarly, let's take another WebElement example.
     <input type="text" name="Username" />
In the preceding code, input is a WebElement, type and name are attributes of WebElement input having text and Username values, respectively.

3. <p> My paragraph </p> Here, <p> is a WebElement.
4. <a href="scientecheasy.html">Scientech Easy</aHere, <a> is a WebElement. 

HTML element with no content is called an empty element. The empty element does not have an end tag. For example, <br> is a WebElement which indicates a line break. This element has no content and does not have an end tag.

List of WebElement Commands


The following methods are the list of the WebElement interface. These methods are invoked through the web elements interface. Let us see a glance at the list of WebElement commands. 

Write the following snippet code in your Eclipse and look at the list of all WebElement commands. 
Program source code:
    package seleniumProject; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class ListOfWebElementCommands { public static void main(String[] args) { WebDriver driver=new FirefoxDriver(); WebElement element=driver.findElement(By.id("Username")); element. } }
A list of all WebElement commands is shown in the below screenshot.

Selenium WebDriver WebElement commands

1. FindElement Command

Methods:
    findElement(By by) : WebElement
➲ Locating an element is the first step before performing any user actions in UI automation. findElement method is a convenient way that is used to locate an element on a web page. It is an abstract method of the web driver interface.

➲ The findElement() method takes an input parameter of type By. The By is an instance of By class.

➲ The return type of findElement() method is a WebElement object that represents an HTML element on the web page. 

➲ This method is used to locate one web element in the current web page using the locator provided in the parameter list. If the multiple elements are found with the same locator, it returns the first web element.

➲ If WebDriver is unable to find an element using the locator provided in the parameter list then it will throw a runtime exception named NoSuchElementException.
Syntax:
         driver.findElement(By.locatorName("Attribute value"));

For example:
    driver.findElement(By.id("India")); // Here, id is a name of locator and India is attribute value of id. //Or WebElement country=driver.findElement(By.id("India")); // Return type is a WebElement.

2. FindElements Command 

Method:
    findElements(By by) : List
➲ It is also an abstract method of web driver interface.
➲ The findElements() method also takes a parameter of type By. It returns a list of WebElements.

➲ This method is used to find a list of web elements (i.e. multiple elements) on the current web page using the locator provided in the parameter list. If more than one web elements are present on the web page that matches the same locator, all of them are returned in a list.
➲ If no elements are found using the given locator then the findElements() method returns an empty list.
Syntax:
        driver.findElements(By.locatorName("Attribute value"));

For example:
    driver.findElements(By.xpath("//*")); // or List<WebElement> elements=driver.findElements(By.xpath("//*"));

3. Click Command

Method:
    click() : void
➲ The click() method accepts nothing as a parameter and returns nothing.
➲ This method is used to click on a web element such as a text element, link, checkbox, radio button or any kind of other buttons.
Syntax:
        driver.findElement(By.locatorName("Attribute value")).click();

For example:
    driver.findElement(By.linkText("Scientech")).click(); // linkText is a name of locator and Scientech is an attribute value of linkText. // or WebElement element=driver.findElement(By.linkText("Scientech")); element.click();

4. Clear Command

Method:
    clear() : void
➲ The clear() method accepts nothing as a parameter and returns nothing.
➲ If the element is a text entry element, this method will is used to clear the value. i.e. this method is used to erase the string value present in the text field.  
Syntax: 
         driver.findElement(By.locatorName("Attribute value")).clear();

For example:
    WebElement element = driver.findElement(By.id("Password")); element.clear(); //Or driver.findElement(By.id("Password")).clear();

5. Submit Command

Method:
    submit() : void
➲ The submit() method accepts nothing as a parameter and returns nothing.
➲ The submit() method is used to submit a form with <form> tags. It is applicable on <form> element or any element that is under <form> element.
➲ It will throw NoSuchElementException if the current element is not within a form.
Syntax:
        driver.findElement(By.locatorName("Attribute value")).submit();

For example:
    driver.findElement(By.id("SubmitButton")).submit(); // or WebElement element=driver.findElement(By.id("SubmitButton")); element.submit();

6. Sendkeys Command

Method:
    sendKeys(CharSequence...) : void
➲ The sendKeys() method accepts text or any sentence as a parameter and returns nothing.
➲ This method is used to send a character sequence to a web element when called on the findElement method. i.e. this method is used to type or insert text in the text field on the runtime. This method has the following syntax:
Syntax:
      driver.findElement(By.locatorName("Attribute value")).sendKeys("Your text");

For example:
The following snippet code is the example of sendKeys() method that will insert text or a sentence in the Google search text field.
    driver.get("https://www.google.com"); WebElement element=driver.findElement(By.name("q")); element.sendKeys("Selenium WebDriver");

7. IsDisplayed Command

Method:
    isdisplayed() : boolean
➲ The isDisplayed() method accepts nothing as a parameter and returns boolean value (true or false).
➲ This method is used to verify whether a certain element is displayed or not on the web page.
➲ This method will return true if the element is present on the web page. Otherwise, it will throw NoSuchElementFound exception if the element is not present on the page.
Syntax:
         driver.findElement(By.locatorName("Attribute value")).isDisplayed(); 

For example:
    boolean status=driver.findElement(By.id("Username")).isDisplayed(); // or WebElement element=driver.findElement(By.id("Username")); boolean status=element.isDisplayed(); System.out.println(status);

8. IsEnabled Command

Method:
    click() : void
➲ The isEnabled() method takes nothing as a parameter and returns boolean value (true or false).
➲ This method is used to verify whether a certain element is enabled or not in the web page.
➲ It will return true if the element is enabled on the page.
Syntax:
         driver.findElement(By.locatorName("Attribute value")).isEnabled();

For example:
    boolean status=driver.findElement(By.id("Username")).isEnabled(); // or WebElement element=driver.findElement(By.id("Username")); boolean status=element.isEnabled(); // Return type of isEnabled method is a boolean. // It can be used as WebElement element=driver.findElement(By.id("Username")); boolean status=element.isEnabled(); // Now check that the element is enabled or not. If it is enabled then enter the value. if(status){ element.sendKeys("Scientech"); // or WebElement element=driver.findElement(By.id("Username")); if(element.isEnabled){ element.sendKeys("Scientech"); }

9. IsSelected Command

Method:
    click() : void
➲ The isSelected() method takes nothing as a parameter and returns boolean value.
➲ This method is used to verify whether a certain checkbox, radio button or option in the drop-down box is selected or not. It does not work with other elements.

➲ It returns true if the element is currently selected otherwise returns false.
Syntax:
         driver.findElement(By.locatorName("Attribute value")).isSelected();

For example:
    // Suppose "India" is radio button. boolean status=driver.findElement(By.id("India")).isSelected; System.out.println(status); // or WebElement element=driver.findElement(By.id("India")); if(element.isSelected){ element.click(); }
For more details about isdisplayed, isEnabled, isSelected methods, click on the below link: Selenium isDisplayed, isEnabled, isSelected Methods
Final words
Hope that this tutorial has covered all the important points related to Selenium WebDriver WebElement commands and methods. I hope that you will have understood Selenium WebElement methods and enjoyed them.
Thanks for reading!
Next ➤ Locators in Selenium ⏪ Prev Next ⏩