- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find elements using Selenium WebDriver?
We can find elements using Selenium webdriver with the help of the findElements method. This can be applied to the locators like id, class, name, link text, partial link text, css, xpath and tagname.
The findElements method returns a list of elements which match with the locator (with the By object) passed as a parameter to the method. To count the number of elements returned by the list, the method size is used. If there are no matching elements, an empty list is returned.
The below image shows the list of methods available with findElements.
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.List; public class FindElmnts{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //URL launch driver.get("https://www.tutorialspoint.com/index.htm"); //identify elements with anchor tagname List e = driver.findElements(By.tagName("a")); //count matching elements with size int s = e.size(); System.out.println("Number of matching elements: " + s); driver.quit(); } }
Output
- Related Articles
- Find elements inside forms and iframe using Java and Selenium WebDriver.
- Clicking on elements within an SVG using XPath (Selenium WebDriver).
- Reading JavaScript variables using Selenium WebDriver.
- How to interact with hidden elements in Selenium Webdriver?
- Switch tabs using Selenium WebDriver with Java.
- How to Verify Tooltip using Selenium WebDriver?
- How to upload files using Selenium Webdriver?
- Where to find Selenium Webdriver release notes?
- Selenium RC vs Selenium webdriver.
- Selenium WebDriver StaleElementReferenceException.
- Test if element is present using Selenium WebDriver?
- How to select checkboxes using selenium java webdriver?
- Get page title with Selenium WebDriver using Java.
- How to deal with ModalDialog using selenium webdriver?
- Capturing browser logs with Selenium WebDriver using Java.

Advertisements