liveBook · Manning

Nested lookup

As explained in BDD in Action, Second Edition

A nested lookup locates a descendant element by searching from an already located parent element.

A nested lookup locates a parent element first, then searches within that WebElement for a descendant. WebDriver supports findElement() and findElements() directly on WebElement instances. section 10.2.12

Prefer nested lookups when they make element selection clearer:

  • Find the main navigation by its ID, then find its Book link by link text. section 10.2.12
  • Use this approach instead of constructing complex XPath or CSS expressions when the parent-child structure is sufficient. section 10.2.12

Selecting the main-menu Book link

driver.findElement(By.id("main-navbar")) .findElement(By.linkText("Book")) .click(); The first lookup establishes the search scope; the second locates and clicks the descendant link. section 10.2.12

How BDD in Action, Second Edition covers it

How deep it goes
Explains the parent-then-descendant technique with chained calls and selector comparisons, stopping short of broader WebDriver internals.
Examples
Demonstrates selecting a main-menu Book link and scoping featured-destination titles to the featured container.
What it assumes
Assumes basic WebDriver element location and CSS or link-text selectors, while teaching searches scoped to WebElement instances.
What you can do after
Apply it via WebDriver to write clearer, more maintainable, and often more reliable element searches.
How it connects
Connects WebDriver, WebElement, CSS selectors, XPath, and link text selectors.

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.