Home > Enterprise >  I am getting this error "stale element reference: element is not attached to the page document&
I am getting this error "stale element reference: element is not attached to the page document&

Time:12-26

I am trying to add multiple products in to the cart

WebElement ele = driver.findElement(By.xpath("//li[text()='Grocery ']"));

//Creating object of an Actions class
Actions action = new Actions(driver);

//Performing the mouse hover action on the target element.
action.moveToElement(ele).perform();
Thread.sleep(3000);
driver.findElement(By.xpath("//li[@title='staples']")).click();
               
Thread.sleep(5000);
              
List<WebElement> products = driver.findElements(By.xpath("//div[@class='product-grid-img']"));
              
for(int i=0;i<products.size();i  )
{
                
    products.get(i).click();
    Thread.sleep(3000);
    driver.findElement(By.xpath("(//span[text()='ADD TO CART'])[1]")).click();
    driver.navigate().back();
    driver.navigate().refresh();
}

CodePudding user response:

As we can see, by clicking on a product a new page is opened so to get back you are navigating to the previous page explicitly and refreshing the page.
When you leaving the original page or refreshing it the elements collected by Selenium on that page becoming no more relevant AKA Stale.
You can read here or at any other online resource about this issue.
To make your code work you will have to get the product again.
Something like this should work:

List<WebElement> products = driver.findElements(By.xpath("//div[@class='product-grid-img']"));
              
for(int i=0;i<products.size();i  )
{                
    List<WebElement> products = driver.findElements(By.xpath("//div[@class='product-grid-img']"));

    products.get(i).click();
    Thread.sleep(3000);
    driver.findElement(By.xpath("(//span[text()='ADD TO CART'])[1]")).click();
    driver.navigate().back();
    driver.navigate().refresh();
    Thread.sleep(3000);
}

CodePudding user response:

Every time you navigate to anywhere, your browser constructs a map of a sorts of the page, in memory. This is called the DOM. When you navigate somewhere else, the DOM is replaced with a new DOM of the new page. The browser does not keep a history of the DOMs you visited, so when you navigate back to somewhere you have already been, the browser has to construct the DOM again.

Let's look at the relevant parts of your code:

List<WebElement> products = driver.findElements(By.xpath("//div[@class='product-grid-img']"));

Find a bunch of elements in the current DOM.

for(int i=0;i<products.size();i  )
{
    products.get(i).click();

First time navigate to another page. So discard all the elements you just found above and rebuild the DOM.

Second time through the loop, you are trying to use an element that is no longer found in the current DOM. Which throws the StaleElementException.

    Thread.sleep(3000);
    driver.findElement(By.xpath("(//span[text()='ADD TO CART'])[1]")).click();
    driver.navigate().back();

Navigate again somewhere. Another rebuild of the DOM.

    driver.navigate().refresh();

Will not help.

}

You will need to restructure your loop to find the element each time. Perhaps something like:

for(int i=0;i<products.size();i  )
{
    // You will have to find the correct XPath here!
    WebElement product = driver.findElement(By.xpath("//div[@class='product-grid-img']["   i   "]"));
    product.click();
    Thread.sleep(3000);   // You might want to use a proper wait here.
    driver.findElement(By.xpath("(//span[text()='ADD TO CART'])[1]")).click();
    driver.navigate().back();
}

CodePudding user response:

StaleElementExceptions are caused by going to a new page and accessing old elements that are no longer valid.

You could do what others did and regrab the values or if it's in an a tag you could just collect all the hrefs and then just driver.get() to them removing the need to driver.navigate().back();

.getAttribute("href")

CodePudding user response:

I just searched for your query in Google to check , with the intention to learn more about what exactly is the question and topic , I got this : "People also ask How do you fix stale element reference element is not attached to the page document in selenium? The element in the DOM is not found because your page is not entirely loaded when Selenium is searching for the element. To solve that, you can put an explicit wait condition that tells Selenium to wait until the element is available to be clicked on. Thanks for this answer.17-Jan-2019" Hope this answer helps you , to some extent .....

  • Related