Stale Object Reference while Navigation using Selenium

后端 未结 3 1115
攒了一身酷
攒了一身酷 2021-01-26 12:16

I have been trying a simple program that navigates and fetches data from the new page, comes back in history and open other page and fetch data and so on until all the links hav

相关标签:
3条回答
  • 2021-01-26 12:35

    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();
    }
    
    0 讨论(0)
  • 2021-01-26 12:36

    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();
            }
        }
    
    0 讨论(0)
  • 2021-01-26 12:37

    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.

    0 讨论(0)
提交回复
热议问题