Testing / Selenium Interview questions
What happens when a Selenium locator doesn't match any element?
The behavior differs depending on which lookup method is used and what waits are configured. With no implicit or explicit wait set, findElement() checks the DOM exactly once and throws NoSuchElementException immediately if nothing matches - there's no retry at all in that bare case.
With an implicit wait configured, findElement() instead polls repeatedly for that wait's duration before finally throwing the same NoSuchElementException if the element still never appears. findElements() behaves differently regardless of waits - it simply returns an empty list rather than throwing anything, since it's designed for existence checks rather than assuming an element must exist.
An explicit WebDriverWait wrapping a presenceOfElementLocated condition instead throws a TimeoutException, not NoSuchElementException, once its own timeout elapses - which is a common source of confusion when reading stack traces from a suite that mixes wait strategies.
More Related questions...