Now we know the reason of occurring StaleElementReferenceException and also unique feature of PageFactory to look for web element every time whenever a method is called on it. Are you able to connect dots? This problem can be solved using PageFactory. Let’s try it.
package StaleElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class GitHubLoginPage {
WebDriver driver;
@FindBy(id = "login_field")
public WebElement username;
@FindBy(id = "password")
public WebElement password;
@FindBy(xpath = "//input[@value='Sign in']")
public WebElement submit;
public GitHubLoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class StaleElementHandling2 {
public static void main(String[] args) throws InterruptedException {
// Setup browser
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://github.com/login");
// Creating object of page class which will initialize web elements
GitHubLoginPage lp = new GitHubLoginPage(driver);
lp.username.sendKeys("amod");
lp.password.sendKeys("dsds");
lp.submit.click();
Thread.sleep(5000);
// Same element
lp.username.sendKeys("amod");
lp.password.sendKeys("dsds");
lp.submit.click();
Tagged as:
How PageFactory Could Help to Handle StaleElementReferenceException StaleElementReferenceException StaleElementReferenceException and pagefactory
Hi Amod,
When I launch browser and navianav to my application I will click on one link which will redirect to a next page. This operation we are doing in each and every test case. But after some 10 to 15 test cases I am getting StaleElementReference Exception even though there is no page refresh (the element which we are clicking is the first element on the webpage) and we are laughing browser for each and every test case. And even though page is getting loaded completely it’s unable to get an element.
Could you please let me know the cause of issue and how can I resolve it as it’s affecting our nightly batch run. And one more our system is also not going to sleeping mode, it will be active only.
Thanks in advance, waiting for your response.
Hello,
Are you using PageFactory concept? If not, implement it.
But you may get StaleElementReference even after using PageFactory. Suppose you click on a link, you dont see page is being refreshed but some ajax calls happen. If WebDriver finds any element in between that and stores it, that reference will go stale when ajax calls are completed. You can use javascript to get page loading status as completed or proper wait.
Thanks
Yes you can get if webdriver searches for element before page is completely loaded or ajax call is in progress or dynamic application. Page object model says webdriver to locate element if page is refreshed but not smart enough to know when should actually start locating. SO its your responsibility to use proper waits as well.
Issue is solved through code by changing while loop to for loop and adding another for loop inside and using find elements along with counter variable,
Hi Amod ,
I am facing issue with staleelement issue from 5days, can u suggest how to update in my code https://stackoverflow.com/questions/44471715/element-not-found-in-cache-perhaps-the-page-has-changed-since-it-has-lookedup-s/44476374#44476374
Pls help
Zareena
Try to do a findElement each time before you use the element. i.e put the line
java.util.List ListPath1 = driver.findElements(By.xpath((ReadPropertyFile.readProperty(“ClickNamePath”))));
inside the for loop. What is happening as Amod explained above that address of elements get updated. So if you put the findElements line inside the for loop you will have the latest address and it may solve the problem
give it a try
Hello,
Yours is not exactly stale exception. You have used click in a loop. Once it clicks and den go back to loop. Click changes tha page. This is causing issue.
Thanks Amod for explaing the reason behind this exception with a very nice example
So we need to search the element again on the page to get the correct addres..
Instead of using @Findy notation and Pagefactory….
Cant we just put a simple find element like done on this page below…
http://toolsqa.com/selenium-webdriver/page-object-model/
public static WebElement username(WebDriver driver){
element = driver.findElement(By.id(“username”));
return element;
is there a difference between the one used by you where we used FindBy and Pagefactory compared to this one where we just do a findElement in the Pageobject class so each time you refer to the element it will be searched again so always correct address would be returned
Thanks Amod. This was much needed article. And once again your simple and easy explanation has cleared all doubts regarding this exception very well. Awesome