0

My code is giving the following error "org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document" .It runs once, but when it goes to the second item, the error happens.

    /**
     * CONSEGUI CAPTURAR OS ITENS A SER COMPRADOS ATRAVES DO PRIMEIRO WEBELEMENT
     * que basicamente fez um elemento web com a div que contem os itens
     * E DEPOIS FIZ UMA LISTA DESSE PRIMEIRO WEBELEMENT
     * aonde filtrei pelo ids que continham a string "add-to-cart-"
     */
    WebElement prod=driver.findElement(By.id("inventory_container"));
    List <WebElement> listofItems = prod.findElements(By.xpath("//button[starts-with(@id,'add-to-cart-')]"));
    for(WebElement product : listofItems){

        prod=driver.findElement(By.id("inventory_container"));
        listofItems = prod.findElements(By.xpath("//button[starts-with(@id,'add-to-cart-')]"));
        
        
        driver.findElement(By.xpath("//button[@id='add-to-cart-sauce-labs-fleece-jacket']")).click();
        product.click();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        driver.findElement(By.xpath("//*[contains(@class, 'shopping_cart_container')]")).click(); 
        driver.findElement(By.xpath("//button[@id='checkout']")).click(); 
        

        CadastraComprador("joao", "costa", "35510");
        driver.findElement(By.xpath("//*[contains(@class, 'submit-button btn btn_primary cart_button btn_action')]")).click();
        

        comparaPrecoTotal("Item total: $65.98");
        buttonFinish.click();
        driver.navigate().to("https://www.saucedemo.com/inventory.html");
    


    }
  • Refer this if it helps - https://stackoverflow.com/questions/18225997/stale-element-reference-element-is-not-attached-to-the-page-document – Shawn Mar 21 '23 at 12:25
  • once you click the DOM is probably going to update which will make your "listofItems" element references all go stale. – pcalkins Mar 21 '23 at 18:09

1 Answers1

0

A few ideas:


First, try using an explicit wait instead of implicit wait. Sometimes, that helps.

If an explicit wait of 20 seconds works, then you would gradually reduce the wait down to 15 seconds, 10 seconds, 5 seconds, ... until you find the shortest working wait time.


Second, if the above doesn't work, what I would normally do is to locate the parent DOM element of the target, and work downward from there. (And, if the parent element doesn't work, try the grandparent element, etc). Target the descendant with css selector "#idOfAncestor *".

Webpages are built using dynamic Javascript nowadays, so there are many ways for an element to become "hidden" or "removed" (or, even worse, become "visible but uninteractable".) Sometimes, it helps to locate the stale elements from the ancestor elements.


If the above two appraoches don't work, sometimes it helps to use other Selenium methods to locate the element, such as:


By the way, do you have any username/password for your https://www.saucedemo.com website? (so that I can try to log in and help) Thanks!

howard_9
  • 413
  • 3
  • 15
  • Make sure you understand what stale element reference means. It's when a reference no longer points to an element that's in DOM – pguardiario Mar 22 '23 at 00:44
  • 1
    Users and password are on Howard's own site, I'll be grateful for the help – joao victor Mar 22 '23 at 01:46
  • I tried in the ways indicated by you, but without success, either by ancestors or by explicit waiting :( – joao victor Mar 23 '23 at 18:54
  • I managed to solve the problem using the explicit wait, but throughout the loop, the list is only on one button, it does not click on others – joao victor Mar 25 '23 at 11:30
  • Hi @joaovictor, you can create a minimum working setup of your project on https://stackblitz.com/, and I'll log in to join you to work through the issues. (By the way, please upvote my answer if it was able to partially help you, thanks!) – howard_9 Mar 25 '23 at 16:24