Stale Element Reference error in Selenium

前端 未结 2 650
无人及你
无人及你 2020-12-22 12:54

I am getting an error while running my selenium tests

Exception in thread \"main\" 
org.openqa.selenium.StaleElementReferenceException: stale element 
refere         


        
相关标签:
2条回答
  • 2020-12-22 13:34

    You should spend some time reading about and understanding what a StaleElementReferenceException is. It is important to understand what causes it and what to do to avoid it.

    In this case, you are scraping the page and loading category with elements off of Page 1. You then click some link that takes you to page 2. At that point, all the references in category are stale but an exception isn't thrown yet because you haven't accessed any of the variables yet. You then use .back() to return to Page 1 and attempt to do something with category and get the exception.

    To avoid this, you need to rescrape the elements into category on Page 1 after you use .back() from another page. One way is what I've written below. The page is scraped at the bottom of each loop.

    List<WebElement> category = driver.findElements(By.className("a2s-skill-block"));
    for (int i = 0; i < category.size(); i++)
    {
        category.get(i).click();
        // sleeps are a bad practice, use WebDriverWait instead
        driver.navigate().back();
        driver.findElement(By.id("iApps")).click();
        // sleeps are a bad practice, use WebDriverWait instead
        category = driver.findElements(By.className("a2s-skill-block"));
    }
    
    0 讨论(0)
  • 2020-12-22 13:57

    You find out all category on the page before for loop. When you click something inside loop to enter another page, Actually the 'Stale Element Refrence' had been triggered, regrardless of you back to the last page finally.

    Below conditions will trigger 'Stale Element Refrence'

    Condition 1

    Use the found element when you enter the page last time and you leave the page at least one time (even you back again).

    You can think as when you enter a page Selenium will assign a reference to the page inside Selenium, Even you entered the same page, but Selenium can't know they are same so it will assign an new reference.

    You can only use found element which page reference is same as current page you are on.

    Condition 2

    Stay on the page(not leave it), but your script triggered the HTML DOM Node of the found elements change/reattach/delete. for example some attribute of the DOM Node changed or the DOM Node removed and added back again desipte all atttibutes not change.

    Thus any change/move on the DOM Node of found elements will trigger 'Stale Element Refrence'. You can think Condition 1 is just another way of change/move DOM Node.

    To fix you issue, you should read out an attribute of all category which can be used to identify the category later in the loop.

    Below code assume each category has unique text:

    List<WebElement> category = 
        driver.findElements(By.className("a2s-skill-block"));
    
    List<String> categoryTexts = new ArrayList<String>();
    
    for(WebElement it: category) {
        categoryTexts.add(it.getText());
    }
    
    for(int i = 0;i<category.size();i++) {
    
        driver.findElement(By.xpath("//*[text()='"+categoryTexts.get(i)+"']")).click();
        Thread.sleep(7000);
    
        driver.navigate().back();
        Thread.sleep(15000);
    
    0 讨论(0)
提交回复
热议问题