'stale element reference: element is not attached to the page document in java

Hey I have problem with my script in java and selenium My script adds products to cart and later remove it from cart, but when i tried to remove products i get "stale element reference: element is not attached to the page document". If I understand well, the element vanish from DOM. I tried await, reoload the page, but still it doesn not work



Solution 1:[1]

I cant remove all products and once. I need to click trash for every single product

public void deleteProduct() {

        WebElement removedProduct = shopDriver.findElement(By.className("remove-from-cart"));
        for (int i = 0; i <= count; i++)
        {
            removedProduct.click();
            mainTest.stopThreed();
            clickArt();
            mainTest.stopThreed();
            pressCartButton();
            mainTest.stopThreed();

        }
        //WebDriverWait wait = new WebDriverWait(shopDriver,10);
        //WebElement removedProduct = wait.until(ExpectedConditions.elementToBeClickable(By.className("remove-from-cart")));
        //removedProduct.click();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Padme