findElement vs findElements in Selenium 4 – Complete Practical Guide (Java)
By Bhau Automation Lab • Selenium 4 WebDriver – Find Single & Multiple Elements
🎯 What You Will Learn in This Video
- How to find single web element using findElement()
- How to find multiple web elements using findElements()
- Difference between findElement and findElements in Selenium
- Handling NoSuchElementException in Selenium
- Using different locators: ID, Name, Class, XPath, CSS
- Practical examples in Selenium 4 with Java
💡 Pro Tip: Use findElements() when you expect multiple matching elements or want to avoid NoSuchElementException.
📌 What is findElement() in Selenium?
findElement() is used to locate a single web element on the web page. It returns the first matching element found by the given locator.
- Returns a single WebElement
- Throws NoSuchElementException if element is not found
- Best used when element is unique
WebElement loginBtn = driver.findElement(By.id("loginBtn"));
loginBtn.click();
📌 What is findElements() in Selenium?
findElements() is used to locate multiple web elements on the web page. It returns a list of WebElements.
- Returns List
- Does NOT throw exception if element not found
- Returns empty list if no elements are found
Listlinks = driver.findElements(By.tagName("a")); System.out.println("Total links: " + links.size());
🔄 Difference Between findElement() and findElements()
| findElement() | findElements() |
|---|---|
| Finds single element | Finds multiple elements |
| Throws NoSuchElementException | Returns empty list if not found |
| Returns WebElement | Returns List |
📍 Locators Used in Selenium
- ID
- Name
- Class Name
- LinkText
- PartialLinkText
- TagName
- XPath
- CSS Selector
❓ Common Interview Questions
Q: What happens if findElement() does not find element?
A: It throws NoSuchElementException.
Q: What does findElements() return if element is not found?
A: It returns an empty list.
🎥 Watch the Complete Video Tutorial
👉 Watch on YouTube: findElement vs findElements in Selenium 4 (Practical Demo)
🎓 Key Takeaways
- Use findElement() for unique elements
- Use findElements() to handle multiple elements
- findElements() avoids NoSuchElementException
- Choosing correct locator improves test stability
- Essential concept for Selenium automation interviews
🚀 Created with ❤️ by Bhau Automation Lab