QA Interview Handbook
  • 🏠Home Page
  • About Testing
    • 💖Qualities of A Great Tester
  • Manual Testing
    • 💡Overview
      • ✋Demand for Software Testing
      • 😄Tester's Role in Manual Testing
      • 7️Testing Principles
      • 🚨V & V
      • ❔Interview Questions
    • ♻️SDLC
      • 📒Phase 1: Planning
      • 🔍Phase 2: Requirement Analysis
      • 👔Phase 3: Design
      • ⛑️Phase 4: Development
      • 🧪Phase 5: Testing
      • 🚚Phase 6: Deployment
      • 🖱️Phase 7: Maintenance
      • ⚔️Common Challenges
      • ❔Interview Questions
    • 🌀STLC
    • 🌊Waterfall
    • ✳️Agile
      • 😁Tester's Role in Scrum
    • 🔢Types
      • ⬜White Box Testing
      • ⬛Black Box Testing
        • 🔰Techniques Used in Black Box Testing
        • 🚘Functional Testing
          • 1️⃣Unit Testing
          • 2️⃣Integration Testing
            • 🔰Techniques Used in Integration Testing
          • 3️⃣System Testing
            • 📼Types of System Testing
            • 🌊Phases of System Testing
            • 🌀Regression Testing
            • 🌫️Smoke Testing
          • 4️⃣Acceptance Testing
            • ⚙️User Acceptance Testing
            • 🅰️Alpha Testing
            • 🅱️Beta Testing
        • 🕳️Non Functional Testing
      • 📑Grey Box Testing
    • 🪄User Story
      • ⏺️Sample User Stories
    • 📓Test Cases
      • ⏺️Sample Test Cases
      • ❔Interview Questions
    • ✖️Defect Life Cycle
      • ☣️Priority + Severity
      • ⏺️Sample Defect Reports
      • ❔Interview Questions
      • 🐛Buggy Questions
    • 🌐Atlassian JIRA
      • 🐞JIRA Issues
      • ❔Interview Questions
    • ❔Interview Questions
  • Accessibility Testing
    • 💡Overview
    • 🤓Tester's Role in Accessibility Testing
    • 📚WCAG Principles
      • 👁️Perceivable
      • 🏹Operable
      • 🧠Understandable
      • 🤖Robust
    • 🔧Axe DevTools
      • ❔Interview Questions
    • 📓Test Cases
    • ❔Interview Questions
  • API Testing
    • 💡Overview
    • 😀Tester's Role in API Testing
    • 🍊HTTP Methods & CRUD
      • 🍂HTTP Status Codes
    • 🍍API Tools
      • 🟠Postman
        • ☄️Sending your first API request
        • 🔬HTTP Requests with Java
        • 🎲GitHub Sample
        • ❔Interview Questions
      • ⛑️REST Assured
        • 🎇Dependency
        • ❔Interview Questions
    • 📓Test Cases
    • 🦧API Cheatsheet
    • ❔Interview Questions
  • Database Testing
    • 💡Overview
    • 😆Tester's Role in Database Testing
    • 🔵SQL
      • ⛓️Constraints
      • 🛢️Referencing a Column
      • 🔼DDL Commands
      • 🔼DML Commands
        • 🖌️Operators
        • 🛠️Functions
          • ⏯️Aggregate Functions
        • 🎅Clauses
          • ⏫Join Clauses
          • 🔵Filter Clauses
          • ⏬Set Operations
      • 🃏Wildcard Character
      • ❔Interview Questions
    • 📓Test Cases
    • 🧤SQL Practice Sites
    • 🫐SQL Cheatsheet
    • ❔Interview Questions
  • Java
    • ⛩️Introduction
    • 😄Tester's Reason to Learn Java
    • Ⓜ️Main Method
      • ❔Interview Questions
    • 📐Variables & Types
      • 🪢Special Types
    • 🥁Modifiers
    • 🅾️Operators
    • 🪡String
      • 🩺String Methods
        • String Method Problems
      • 🚨Delimiter
      • ❔Interview Questions
    • 🖇️Conditionals
      • 💎Common If Statements
      • 💎Common Ternary Operator Statements
    • Ⓜ️Math Class
    • 🌊Loops
      • 💎Common Loop Examples
      • 🔃Nested For Loops
    • 🍼OOPS
      • 🏛️Classes and Objects
        • ❔Interview Questions
      • 🎃Constructor
        • ⚡Static
          • ❔Interview Questions
        • 📍This() & Super()
          • ❔Interview Questions
        • 🍀Finalization
      • 🔓Encapsulation
      • 🐥Inheritance
      • 🦋Polymorphism
      • 🕸️Abstraction
    • 🏮Java Practice Sites
    • ☑️Data Structures + Algorithms
      • 🅾️Big O
      • ☑️Data Structures
        • 🔸Array
        • 🔳Array Problems
        • Page
      • 🪟Sliding Window Technique
        • 🪟Sliding Window Problems
        • 🥐LeetCode #53
        • 🥐LeetCode #209
    • ❔Interview Questions
  • Automation Testing
    • 🚰Flow
      • 💡Overview
      • 🤩Tester's Role in Automation Testing
      • 🍀Selenium
        • 🕸️Selenium WebDriver
          • 🕷️WebDriver Commands
            • 🌐WebElement
              • 🔆HTML Tags
              • 🔬Find Element(s)
              • 🦎Locators
                • ❌Xpath
                • 🏰CSS Selector
                • 📀DOM
                • 🏠Quick Reference for XPath + CSS
            • ✋Waits
            • Browser Management
            • 🏎️Navigation
            • Alerts
          • 🏷️Advanced User Interactions
            • 🗯️Action vs. Actions
            • 💧Drop Down
            • ✅Check Box
            • 🖇️Forms
          • ⚠️Exceptions
        • 🍏OOPS + Selenium
        • 🚢Frameworks
          • ⚓Module Based Framework
          • 🎹Keyword Driven Framework
          • 🎋Data Driven Framework
          • 🌺Hybrid Framework
          • 🌴Log4j
          • 📄Page Object Model
        • 🧪Testing Frameworks
          • 💡TestNG
          • 🉑JUnit
          • 🥒BDD
            • 🥒Cucumber
        • 🌉Selenium Grid
          • ✖️Desired Capabilities
        • ❔Interview Questions
      • 🔄API Testing with Selenium
      • ⏪Database Testing with Selenium
      • Ⓜ️Maven
      • 🐙Git
        • ❔Interview Questions
      • 🕵️‍♂️Jenkins
        • ❔Interview Questions
      • 🐳Docker
        • ❔Interview Questions
      • 📙AWS
        • ❔Interview Questions
  • Behavioral
    • 📣Mixed Interview Questions
    • ⭐STAR Method
      • 🌟Sample Responses
Powered by GitBook
On this page
  • Key things about findElement:
  • 🎯 Rules of findElement
  • ⚙️ Relation to OOPs Concepts ⚙️
  • 📏 Best Practices
  • Some example usage in Java:
  • So in summary:
  • Find Elements
  • Here are some of the differences between findElement() and findElements() methods:

Was this helpful?

  1. Automation Testing
  2. Flow
  3. Selenium
  4. Selenium WebDriver
  5. WebDriver Commands
  6. WebElement

Find Element(s)

PreviousHTML TagsNextLocators

Last updated 1 year ago

Was this helpful?

The findElement method in Selenium WebDriver is used to locate a single WebElement on a web page.

Key things about findElement:

  • 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.

🎯 Rules of findElement

  • Accepts locator strategies like id, xpath, cssSelector 👍

  • Returns first matching WebElement object 🔍

  • Throws NoSuchElementException if no match 🚨

  • WebElement can call click(), sendKeys() etc. ✅

WebElement searchBar = driver.findElement(By.id("search")); 
searchBar.sendKeys("Selenium");

⚙️ Relation to OOPs Concepts ⚙️

🦾 Abstraction

  • WebDriver defines abstract findElement method

  • Concrete classes like ChromeDriver implement it

💡 Polymorphism

  • Supports poly morphic behavior based on WebDriver reference but ChromeDriver object

👪 Inheritance

  • ChromeDriver, FirefoxDriver inherit findElement from WebDriver

🏭 Class

  • WebElement class encapsulates reusable methods like click(), sendKeys()

📏 Best Practices

  • 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!

Some example usage in Java:

// Find element by ID
WebElement elem = driver.findElement(By.id("myElement"));

// Find element by XPath 
WebElement elem = driver.findElement(By.xpath("//div[@class='myclass']"));

// Interact with returned WebElement
elem.click(); 
elem.sendKeys("Text");

So in summary:

  • 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

Find Elements

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.

The syntax for the findElements() method is as follows:

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

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:

List<WebElement> textInputs = driver.findElements(By.cssSelector("input[type='text']"));

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.

Here are some of the differences between 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:

WebElement element = driver.findElement(By.id("someId"));

Find multiple elements by class name and iterate:

List<WebElement> elements = driver.findElements(By.className("someClass"));
for (WebElement element : elements) {
    element.click();
}

Find a single element but use a list in case more than one is found:

List<WebElement> elements = driver.findElements(By.id("someId"));
WebElement element = elements.get(0);

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.

🚰
🍀
🕸️
🕷️
🌐
🔬