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
WebElementreferences 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 catchStaleElementReferenceExceptionto 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.