Web Automation Using Selenium
  • About The Book
  • Selenium
    • Introduction
      • What is testing and why?
      • What is Selenium and Why?
    • Getting Started
      • Audience
      • Prerequisite
      • Set up
      • Getting Started - Hello World..!!
      • What are drivers
    • Locators
      • CSS Selectors
      • XPath Selector
    • WebDriver
      • WebDriver Methods
      • WebDriver Types
    • WebElement(s)
      • WebElement Methods
      • Looping Through WebElements
    • Waits In Selenium
  • TestNG
    • Introduction
    • TestNG Setup
    • First Test Script
    • Some of the features of TestNG
    • TestNG Annotations
      • @Test
      • @DataProvider
  • Maven
    • Introduction
  • Framework Development
    • Introduction
    • Building Framework
      • Technology Stack
      • Page Object Model (POM)
      • Setup
      • What our framework contain
      • Packages
        • Context
        • Factory
        • Listeners
        • Pages
      • Creating first automation test
  • Common Interview Questions
    • Selenium-Related
Powered by GitBook
On this page
  • WebElements
  • Way to find WebElemets:

Was this helpful?

  1. Selenium

WebElement(s)

WebElement Interface

PreviousWebDriver TypesNextWebElement Methods

Last updated 5 years ago

Was this helpful?

WebElement is an interface in selenium which will a representation of any object we see on a web page. For example button, input box, link, text, image, etc.,

Since it an interface and we'll not be able to instantiate it. But we can get the WebElement object from the driver that we already have.

WebElement element = driver.
                     findElement(By.id("id-of-the-elelemnt"));

By class

By is one more important to class which provides or describes the mechanism from which we are identifying a particular element in a web page. It has all the locator mechanism that we discussed in .

Example:

WebElement element = driver.
                     findElement(By.id("id-of-the-elelemnt"));
                     
WebElement element = driver.
                     findElement(By.className("className-of-the-elelemnt"));

WebElement element = driver.
                     findElement(By.tagName("tagName-of-the-elelemnt"));

WebElement element = driver.
                     findElement(By.xpath("xpath-of-the-elelemnt"));
                     
WebElement element = driver.
                     findElement(By.cssSelector("css-of-the-elelemnt"));

Etc.,

Now the element is the object representation of the element in the web page.

WebElements

Many a times we come across situations we want to interact with similar kind of elements in a single page which share same locators, or there will be multiple elements matched for the locator that we have written and we wanna filter it, in any case finding WebElements instead of finding a single web element will be handy. WebElements is no class.

Way to find WebElemets:

List<WebElement> element = driver.
            findElements(By.id("common-id-of-the-elelemnts"));

Finding a list of web elements has lots of benefits than finding a single element in many situations, Some of them are 1. Finding all options of a select block. 2. Finding all elements of ordered or un-ordered list. Etc.,

Once we find the WebElement, we can call methods corresponding to the actions the we would perform on an element in a web page. Like click, enter text, copy text, drag, drop etc.,

And there are many ways we can leverage list of

WebDriver
Locators
More
WebElements