liveBook · Manning

WebElement

As explained in BDD in Action, Second Edition

A web element is an individual element in a web document that can be located and interacted with through browser automation.

An automated login check must do more than open a page: it must find the login link, enter credentials, submit the form, and verify who logged in. In Selenium WebDriver, findElement() returns a WebElement representing one page element. That object is the handle used to interact with or inspect it. section 10.2.11

Choose a locator that identifies the intended element:

  • Use an HTML id or name for a significant field or button when available. These attributes are less likely to change with page layout than a structural locator. section 10.2.12
  • Use link text for a recognizable link, or a CSS selector or XPath expression when the element needs a more specific match. section 10.2.12
  • Narrow a search to a containing WebElement when the same kind of element appears elsewhere on the page. section 10.2.12

Log in and check the result

After opening the application, driver.findElement(By.linkText("Login")).click() follows the login link. Locate the email and password fields by id and call sendKeys() on each; then locate and click the login button. To check the result, read the current-user element with getText() and compare it with the expected email address. section 10.2.11

A WebElement supports common user actions and checks: click() simulates a click, sendKeys() simulates typing, and getText() reads visible text content. Clear an existing text-field value before typing a replacement. To inspect a field’s current value, read its value attribute with getAttribute("value"). A select field can be passed to Selenium’s Select helper. section 10.2.13

Check a group of destinations

When a home page should show three featured destinations, findElements(By.cssSelector(".featured-destination")) returns a list of WebElement objects whose size can be checked. Find title elements under #featured, read each with getText(), and check that the resulting titles include Singapore. section 10.2.12

In Serenity/JS, the corresponding single-element abstraction is PageElement, not Selenium’s WebElement. A PageElement can be located with a By selector or described relative to a containing PageElement—useful when a repeated widget needs a specific message identified. section 15.2.2

How BDD in Action, Second Edition covers it

How deep it goes
Works through locating, interacting with, and inspecting single elements and collections; does not examine WebElement internals.
Examples
Demonstrates login fields and a current-user assertion, featured-destination counts and titles, and registration-form controls.
What it assumes
Assumes basic familiarity with web-page fields and test code while teaching WebDriver element location and interaction through examples.
What you can do after
Apply it via Selenium WebDriver to locate page elements, simulate user input, and assert displayed results.
How it connects
Connects WebDriver, element locators, CSS selectors, XPath, browser assertions, and Serenity/JS PageElement.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of BDD in Action, Second Edition. The section links point to the parts of the book it draws on.