Stale Object Reference while Navigation using Selenium

元气小坏坏 提交于 2019-12-02 07:46:27

Generally we will be getting the Stale Exception if the element attributes or something is changed after initiating the webelement. For example, in some cases if user tries to click on the same element on the same page but after page refresh, gets staleelement exception.

To overcome this, we can create the fresh webelement in case if the page is changed or refreshed. Below code can give you some idea.

Example:

 webElement element = driver.findElement(by.xpath("//*[@id='StackOverflow']"));
 element.click();
 //page is refreshed
 element.click();//This will obviously throw stale exception

To overcome this, we can store the xpath in some string and use it create a fresh webelement as we go.

String xpath = "//*[@id='StackOverflow']";
driver.findElement(by.xpath(xpath)).click();
//page has been refreshed. Now create a new element and work on it
driver.fineElement(by.xpath(xpath)).click();   //This works

In this case, we are collecting a group of webelements and iterating to get the text. But it seems there is some changes in the webelement after collecting the webelements and gettext throws staleness. We can use a loop and create the element on the go and get text.

for(int i = 0; i<5; i++)
{
  String value = driver.findElement(by.xpath("//.....["+i+"]")).getText);
  System.out.println(value);
}

Hope this helps you. Thanks.

The reason you get StaleElementReference Exception, is normally because you stored element(s) into some variable, however after that you did some action and page has changed (due to some ajax response) and so your stored element has become stale.

The best solution is not to store element in any variable in such case.

This should work.

links = driver.findElements(By.xpath("//*[@id='gvSearchResults']/tbody/tr/td[1]/a"));

for (int j = 0; j < links.size(); j++) {
    System.out.println(links.get(j).getText() + ", ");
    driver.findElements(By.xpath("//*[@id='gvSearchResults']/tbody/tr/td[1]/a")).get(j).click();
    System.out.println("Afte click");
    driver.findElement(By.id("ctl00_MainContent_btnBack")).click();
    this.search();
}

Please check this code

   private void extractText() {
        try {
            List<WebElement> rows = driver.findElements(By.xpath("//*[@id='gvSearchResults']/tbody/tr"));
            List<WebElement> links = null;
            System.out.println(rows.size());
            for (int i = 0; i < rows.size(); i++) {
                links = driver.findElements(By.xpath("//*[@id='gvSearchResults']/tbody/tr/td[1]/a"));
                WebElement ele= links.get(0);
                System.out.println(ele.getText() + ", ");
                ele.click();
                System.out.println("After click");
                driver.findElement(By.id("ctl00_MainContent_btnBack")).click();                     
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!