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
