Selenium – Element not found in the cache

You probably will come across the following error frequently when writing Selenium test case for a Ajax-rich website.

  • Error: Element not found in the cache – perhaps the page has changed since it was looked up

 

This is because the Selenium WebDriver reference to your element would now be stale as the DOM has been modified by Ajax. To resolve the problem in Java, you can make us of the WebDriverWait and find the element each time before you use it.

public void clickAnElementByLinkText(String linkText) {
  new WebDriverWait(driver, 25).until(ExpectedConditions.presenceOfElementLocated(By.linkText(linkText)));
  driver.findElement(By.linkText(linkText)).click();
}

 

Done =)

Reference: StackOverflow – Selenium Webdriver with Java: Element not found in the cache – perhaps the page has changed since it was looked up

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.