OOPS! from interaction: Most Common Exceptions

If the application you are testing against is a dynamic app, most likely you are going to run into a lot of exceptions when you run your script. And most of those exceptions happens during interaction. So let’s take a look at the most common exceptions:

StaleElementReferenceException
Root cause:the element no longer appears on the DOM of the page.
A StaleElementException is thrown when the element you were interacting is destroyed and then recreated. This is closely related to Ajax calls, which we will cover in detail in dedicated chapter for Ajax Handling.

WebDriverException:Element is not clickable at point (xxx, xxx)
Root cause: The element is not visible to click.

Likely to happen when:

  • A play button won’t show until you hover over to its area.
    To address, use Actions to hover over the box first:

    WebElement element = driver.findElement(By(“element_path”));
    Actions actions = new Actions(driver);
    actions.moveToElement(element).click().perform();
  • Need to scroll down the page in order to make the element visible.
    To address, use JavascriptExecutor to scroll the page so that element is visible:

    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript(“scroll(250, 0)”); // if the element is on top.
    jse.executeScript(“scroll(0, 250)”); // if the element is on bottom.

    or:

    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript(“arguments[0].scrollIntoView()”, Webelement);

    Then click on the element.

  • The element is covered by another layer such as running ad, a popup box etc.
    To address, make the page to wait for few seconds using WebDriverWait or FluentWait till the top layer running ad disappears, or dismiss the popup explicitly.

ElementNotVisibleException
Root cause: the element’s locator matches with more than 1 element; or the element is indeed not in not currently visible. Make your locator more specific, or make sure that the element is visible.

MoveTargetOutOfBoundsException
Indicates that the target provided to the actions move() method is invalid – outside of the size of the window.

ElementNotSelectableException
Element may be disabled

InvalidSelectorException
Selector used to find an element does not return any WebElement. Double-check your locator to make sure it is valid.

NoSuchElementException

WebDriver is unable to locate the element. Make sure that your locator is valid.

NoSuchFrameException
WebDriver is switching to an invalid frame, which is not available.

NoAlertPresentException
WebDriver is switching to an invalid alert, which is not available.

NoSuchWindowException
WebDriver is switching to an invalid window, which is not available.
StaleElementReferenceException: The referenced element is no longer present on the DOM page (reference to an element is now Stale). E.g. The Element belongs to a different frame than the current one OR the user has navigated away to another page.

SessionNotFoundException
The WebDriver is performing the action immediately after ‘quitting’ the browser.