Horje
FindElement and FindElements in Selenium

In Selenium, FindElement and FindElements are essential methods used for locating web elements on a webpage. These methods allow you to interact with the elements and perform various actions such as clicking, entering text, and retrieving information.

What is FindElement?

FindElement is Used to locate a single web element on the webpage. This method returns the first matching element based on the specified locator strategy. It always returns a single element and in case of multiple elements, it returns the first element. If no element is found, it throws a NoSuchElementException.

Example: WebElement element = driver.findElement(By.id("elementId"));

What is FindElements?

FindElements is used to locate multiple web elements on the webpage. This method returns a list of all matching elements based on the specified locator strategy. It returns a single element in case of one element found and in case of multiple elements, it returns all the elements. If no elements are found, it returns an empty list rather than throwing an exception.

Example: List<WebElement> elements = driver.findElements(By.className("elementClass"));

Difference between FindElement and FindElements in Selenium

Here are the following differences between FindElement and FindElements in Selenium:

Aspect

FindElement

FindElements

Purpose

FindElement Locate a single web element

FindElements Locate multiple web elements

Return Type

FindElement returns a WebElement

FindElements return List<WebElement>

Locator Strategies

FindElement supports ID, name, class name, etc.

FindElements supports ID, name, class name, etc.

Behavior if Not Found

FindElement throws NoSuchElementException

FindElements returns an empty list

First Matching Element

It returns the first matching element

It returns all matching elements

Example

WebElement element = driver.findElement(By.id(“elementId”));

List<WebElement> elements = driver.findElements(By.className(“elementClass”));

Common Use Cases

Actions on a single element (click, type)

Actions on multiple elements (iteration, bulk actions)

FindElement in Selenium command Syntax (with explanation)

Syntax of FindElement

WebElement element = driver.findElement(By.locator("value"));

Explanation of Syntax of FindElement

  • WebElement: The type of object that represents a web element on a webpage.
  • element: The variable name for the web element you are locating.
  • driver: The instance of WebDriver you are using to control the browser.
  • findElement: The method used to locate a single web element.
  • By.locator(“value”): The strategy for finding the element. By is a class providing various locator strategies.

Various Locator Strategies

Various Locator Strategies are:

  • By.id(“elementId”): Locate by the element’s ID attribute.
  • By.name(“elementName”): Locate by the element’s name attribute.
  • By.className(“elementClass”): Locate by the element’s class name.
  • By.tagName(“tagName”): Locate by the element’s tag name.
  • By.linkText(“linkText”): Locate by the exact text of a link.
  • By.partialLinkText(“partialText”): Locate by a portion of the link text.
  • By.cssSelector(“cssSelector”): Locate using a CSS selector.
  • By.xpath(“xpathExpression”): Locate using an XPath expression.

Example

WebElement loginButton = driver.findElement(By.id("loginButton"));
  • WebElement loginButton: This declares a variable loginButton of type WebElement.
  • driver.findElement: This calls the findElement method on the driver object.
  • By.id(“loginButton”): This uses the ID locator strategy to find the element with the ID “loginButton”.

Find Elements in Selenium command Syntax

Syntax of FindElements

List<WebElement> elements = driver.findElements(By.locator("value"));

Explanation of Syntax

  • List<WebElement>: A list that holds multiple WebElement objects.
  • elements: The variable name for the list of web elements you are locating.
  • driver: The instance of WebDriver you are using to control the browser.
  • findElements: The method used to locate multiple web elements.
  • By.locator(“value”): The strategy for finding the elements. By is a class providing various locator strategies.

Example

List<WebElement> buttons = driver.findElements(By.className("buttonClass"));
  • List<WebElement> buttons: This declares a variable buttons that holds a list of WebElement objects.
  • driver.findElements: This calls the findElements method on the driver object.
  • By.className(“buttonClass”): This uses the class name locator strategy to find all elements with the class name “buttonClass”.

Related Articles:

Conclusion

In conclusion, the findElement and findElements methods in Selenium are used to locate web elements on a webpage. findElement is for finding a single element and will throw an exception if no element is found. In contrast, findElements is for finding multiple elements and will return an empty list if no elements match the criteria. Choose findElement when you expect a single match and findElements when you need to interact with multiple elements.




Reffered: https://www.geeksforgeeks.org


Automation Testing

Related
60 Days Of Software Testing– A Complete Guide For Beginners 60 Days Of Software Testing– A Complete Guide For Beginners
CRUD JUnit Tests for Spring Data JPA CRUD JUnit Tests for Spring Data JPA
What are Exclude and Include Test Methods in TestNG? What are Exclude and Include Test Methods in TestNG?
TestNG Annotations in Selenium Webdriver with Examples TestNG Annotations in Selenium Webdriver with Examples
Benefits of Automation Testing Benefits of Automation Testing

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
18