Selenium WebElement | Selenium findElement

What is WebElement?

The elements that are present on a web page are called WebElement in Selenium. 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 that is present on the label element. </label> is the end tag that represents the 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</a> Here, <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 that indicates a line break. This element has no content and does not have an end tag.

List of WebElement Commands in Selenium


The following methods are the list of WebElement interface. These methods are invoked through 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 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.

WebElement commands in Selenium

Selenium FindElement Command


The general form FindElement() method is as follows:

Method:

findElement(By by) : WebElement

a) 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.

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

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

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

e) 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.


The general syntax to call findElement() method provided by WebElement is as follows:

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.

Selenium FindElements Command


The general form of findElements() method is as follows:

Method:

findElements(By by) : List

a) It is also an abstract method of web driver interface.

b) findElements() method also takes a parameter of type By. It returns a list of WebElements.

c) 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.

d) If no elements are found using the given locator then the findElements() method returns an empty list. The general syntax to call findElements() method is as follows:

Syntax:
    driver.findElements(By.locatorName("Attribute value"));

For example:
    driver.findElements(By.xpath("//*")); 

// Or, 
    List<WebElement> elements = driver.findElements(By.xpath("//*"));

Click Command


The general form of click() method is as follows:

Method:

click() : void

a) The click() method accepts nothing as a parameter and returns nothing.

b) This method is used to click on a web element such as a text element, link, checkbox, radio button, or any kind of other button.

The general syntax to call click() method is given below:

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();

Clear Command


The general form of clear() method is as:

Method:

clear() : void

a) The clear() method accepts nothing as a parameter and returns nothing.

b) 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.

The syntax to call clear() method is given below:

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();

Submit Command


Method:

submit() : void

a) The submit() method accepts nothing as a parameter and returns nothing.

b) 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.

c) It will throw NoSuchElementException if the current element is not within a form.

The general syntax to call submit() method is as follows:

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();

Selenium Sendkeys Command


Thw general form of this method is as follows:

Method:

sendKeys(CharSequence...) : void

a) The sendKeys() method accepts text or any sentence as a parameter and returns nothing.

b) 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");

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");

IsDisplayed Command


Method:

isdisplayed() : boolean

a) The isDisplayed() method accepts nothing as a parameter and returns boolean value (true or false).

b) This method is used to verify whether a certain element is displayed or not on the web page.

c) 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.

The general syntax to call isDisplayed() method is as follows:

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);

IsEnabled Command


Method:

isEnabled() : boolean

a) The isEnabled() method takes nothing as a parameter and returns boolean value (true or false).

b) This method is used to verify whether a certain element is enabled or not on the web page.

c) It will return true if the element is enabled on the page. The general syntax to use isEnabled() method is as follows:

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"); 
    }

IsSelected Command


Method:

isSelected() : boolean

a) The isSelected() method takes nothing as a parameter and returns boolean value.

b) 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.

c) It returns true if the element is currently selected otherwise returns false. The syntax to use isSelected() method is as follows:

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(); 
      }

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 the basic points of Selenium findElements methods.
Thanks for reading!!!

⇐ PrevNext ⇒

Please share your love