🔬Find Element(s)
Last updated
Last updated
The findElement method in Selenium WebDriver is used to locate a single WebElement on a web page.
It is a method that belongs to the WebDriver interface in Selenium.
It takes a locator strategy such as By.id, By.xpath, By.cssSelector, etc as a parameter to identify the element.
It returns a WebElement object, which represents a DOM element on the page.
Only finds the first matching element.
If no element matches the locator, it throws a NoSuchElementException.
The related method is findElements (plural) which returns all matching elements.
Accepts locator strategies like id, xpath, cssSelector 👍
Returns first matching WebElement object 🔍
Throws NoSuchElementException if no match 🚨
WebElement can call click(), sendKeys() etc. ✅
WebDriver defines abstract findElement method
Concrete classes like ChromeDriver implement it
Supports poly morphic behavior based on WebDriver reference but ChromeDriver object
ChromeDriver, FirefoxDriver inherit findElement from WebDriver
WebElement class encapsulates reusable methods like click(), sendKeys()
Use unique locator strategies like ID or xpath 🎯
Wrap findElement in try/catch block 🚧
Avoid repeating findElement. Store once and reuse 🔂
So findElement leverages OOPs concepts like abstraction, polymorphism, inheritance to enable element interaction!
findElement allows locating a single element on the page using a locator strategy
It returns a WebElement object representing that DOM element
This enables further interactions like clicking, entering text, etc.
It is one of the most common Selenium commands for web automation testing
The findElements()
method in Selenium Java is used to find a list of web elements that match a given locator. The return type of the findElements()
method is a List<WebElement>
, which is a collection of web elements.
findElements()
method is as follows:where driver
is the WebDriver instance and locator
is the locator that is used to identify the web elements.
For example, the following code would find all the input
elements with the type
attribute of text
on the current web page:
The findElements()
method can be used to find a list of web elements that match a variety of locators, including:
ID
Name
Class name
XPath
CSS selector
The findElements()
method is often used in Selenium automation scripts to find a list of web elements that need to be interacted with. For example, a script might use the findElements()
method to find all the button
elements on a web page and then click on each button.
findElement()
and findElements()
methods:findElement()
returns a single web element, while findElements()
returns a list of web elements.
findElement()
throws a NoSuchElementException
if the element is not found, while findElements()
returns an empty list if the elements are not found.
findElement()
is typically used when you need to interact with a single web element, while findElements()
is typically used when you need to interact with a list of web elements.
findElement():
Finds a single element on the page. If it finds more than one matching element, it will throw an exception.
Returns a WebElement object which can be used to interact with the dom element.
findElements():
Finds all matching elements on the page and returns a List of WebElement objects.
It will return an empty list if no elements are found. It will not throw an exception if more than one match is found.
You can then iterate over the list of WebElements and interact with each element.
So in short:
findElement() - Finds a single element, and throws an exception if more than one is found. Returns a WebElement.
findElements() - Finds all matching elements, and returns a List of WebElements. Returns an empty list if no match is found.
Some usage examples:
Find a single element by ID:
Find multiple elements by class name and iterate:
Find a single element but use a list in case more than one is found:
This will not throw an exception if more than one match is found, it will simply pick the first element from the list.
If there are no matching elements within the web page, findElements returns an empty list, whereas if there is no element found findelement will return noSuchElementException.