Sunday, 7 August 2022

org.openqa.selenium.StaleElementReferenceException

 org.openqa.selenium.StaleElementReferenceException

When it occurs ? 

  • When the element has been deleted entirely.
  • When the element is no longer attached to the DOM

Causes: 

  • When the page is refreshed 
  • When the user is navigated to another page
  • When the java script deletes the element and creates the same element with the same id then the driver may not identify the replacement

How to overcome ? 

  • By refreshing the web page | 
driver.navigate().refersh(); 
driver.findElement(By.xpath("xpath")).click();
  • By using Try Catch Block
  • By using ExpectedConditions.refreshed
WebDriver wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(ExpectedConditions.refreshed(ExpectedConditions.stalenessOf("ThisIsElement");

  • By using POM Lazy Initialization
    •  In POM, we use initElements() method which loads the element but it won’t initialize elements. 
    •  initElements() takes latest address. 
    •  It initializes during run time when we try to perform any action on an element. 
    •  This process is also known as Lazy Initialization.

No comments:

Post a Comment