Skip to content
SDETRole
SDETRole

Fix a StaleElementReferenceException

intermediate Stale elements Challenge 7 of 13

The helper below collects the product rows, clicks Sort by price, then reads the first row. It throws StaleElementReferenceException because the app re-renders the table body when sorting.

Requirements

  • Do not reuse WebElement references collected before the sort.
  • After clicking sort, wait for an element captured before the sort to go stale with ExpectedConditions.stalenessOf.
  • Then re-locate the rows (for example with visibilityOfAllElementsLocatedBy) and return the first product’s name.
  • Do not use Thread.sleep, and do not catch StaleElementReferenceException to retry.

Expected output

The method keeps a reference to an old row, clicks sort, waits for `stalenessOf(oldRow)`, then re-locates the rows by their `By` locator before reading the first product name.

Checks (4)

  • ○Waits for the old element to become stale
  • ○Uses an explicit WebDriverWait
  • ○Re-locates the rows after the re-render
  • ○No sleeps and no catch-and-retry of the stale exception
Solution.javaValidated · not executed

Ctrl/⌘ + Enter to validate · your code is saved in this browser

Results

Validate your code against the checks.

Type to search across SDETRole.

Full-text search by Pagefind · runs in your browser Open search page →