0

I'm trying to automate a testcase, where in there are around 30 links in a page. All these links will lead to a new page. And depending upon on a value in the new page, I need to either proceed or need to come back to the original page and re-select the link. This needs to be done until I find a particular value in the new page.

So, I got a List of the links, then I started iterating, but as soon as I navigate back, either using driver.navigate().back() or another option and then try to find & click the next link, it hits StaleElementException.

Any idea how I can handle this situation? Any help, code or approach wise would be highly appreciated!

1

When you return back to the page, page reloads and all elements become stale. For you it may look like same element (with same locator), but page is new object.

So you need to fetch the elements again.

0

Main Cause of StaleElementException is:

  1. Your element is no longer attached with the DOM(due to lots AJAX calls or Java script calls)
  2. Element deleted from the DOM,and no longer available on it.

Below are the some possible solutions:

  1. Refreshing Page:

    driver.navigate().refersh(); driver.findElement(By.id("SomeID")).click();

  2. Use conditional wait:

    WebDriverWait wait = new WebDriverWait(driver,10); wait.until(ExpectedConditions.refreshed(ExpectedConditions.stalenessOf(driver.findElement(By.xpath("<xPath>")))));

  3. Another suggestion would be to, referencing the locator as a string and using it, like.

    String locator = String.format("div#someId"); WebElement ele = driver.findElement(By.cssSelector(locator));

Hope that helps you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.