0

I was trying to automate the following scenario:

  • go to amazon.com
  • search for headphones
  • add all the bestsellers in the first results page to the cart

The steps I've followed to script this scenario:

  • go to amazon.com
  • enter the text "headphones" in the search field
  • click on search button
  • click on a link that is tagged as 'bestseller'
  • click on 'add to cart' button
  • navigate back to the results page
  • click on another link that is tagged as 'bestseller'
  • click on 'add to cart' button
  • navigate back to the results page

All the bestsellers has the same xpath:

//span[.='Best Seller']/../../../../../../../../following-sibling::div/div/following-sibling::div/div/div/div/div/div/h2/a/span

So I've implemented this a list of WebElements as follows:

List<WebElement> bestsellers = driver.findElements(By.xpath("xpath of bestsellers"));

I've implemented the clicking on a link and adding to the cart using loop in 3 ways as follows:

for(WebElement product: bestsellers) {
    product.click();
    clickOnAddToCartButton();
    driver.navigate().back();
}



for(int i=0; i<bestsellers.size(); i++) {
        System.out.println(bestsellers.size());
        bestsellers.get(i).click();
        clickOnAddToCartButton();
        driver.navigate().back();

    }



Iterator<WebElement> i = bestsellers.iterator();
    while(i.hasNext()) {
        WebElement product = i.next();
        wait.until(ExpectedConditions.elementToBeClickable(product));

        product.click();
        clickOnAddToCartButton();
        driver.navigate().back();
    }

There are 3 elements in the list 'bestsellers' When I had run the script. When the loop is executed, the first element is getting clicked and added to the cart and the driver navigates back to the results page. Then I'm getting staleElementReferenceException using the above 3 ways.

Update: I've implemented the scenario as follows:

for(int i=0; i<bestsellers.size(); i++) {

        System.out.println("Current :" + i);
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//span[.='Best Seller']/../../../../../../../../following-sibling::div/div/following-sibling::div/div/div/div/div/div/h2/a/span")));
        driver.findElements(By.xpath(".//span[.='Best Seller']/../../../../../../../../following-sibling::div/div/following-sibling::div/div/div/div/div/div/h2/a/span")).get(i).click();
        clickOnAddToCartButton();
        //clickOnViewCart();
        try {
            wait.until(ExpectedConditions.elementToBeClickable(cartButton));
        }catch(TimeoutException e) {
            wait.until(ExpectedConditions.elementToBeClickable(viewCartButton));
        }
        if(i==(bestsellers.size()-1)) {
            try {
                wait.until(ExpectedConditions.elementToBeClickable(cartButton));    
                cartButton.click();
                break;
            }catch(TimeoutException e) {
                wait.until(ExpectedConditions.elementToBeClickable(viewCartButton));    
                viewCartButton.click();
                break;
            }
        }

        driver.navigate().back();
5
  • driver.navigate().back(); - This will refresh the elements on the page which is causing this staleElementReferenceException. Commented Aug 1, 2019 at 15:46
  • ok. thanks for the info. Could you please suggest me a workaround? Commented Aug 1, 2019 at 15:55
  • Should I open the link new tab and close the tab when I'm done with it? Commented Aug 1, 2019 at 15:56
  • In this way, I can come back to the results page without refreshing the elements. Commented Aug 1, 2019 at 15:56
  • you might consider storing the hrefs (url to the individual product) in a list and then navigate to the hrefs, click add to cart, navigate the the next href, etc... Commented Aug 1, 2019 at 17:43

1 Answer 1

1

The moment you click on the element or back() in the browser the element reference will updated in the selenium so you can not point to the elements with the old references and which led to the StatleElementException.

Consider this approach when you have to iterate through multiple elements interaction.

List<WebElement> bestsellers = driver.findElements(By.xpath("xpath of bestsellers"));
for(int i=0; i<bestsellers.size(); i++) {
    System.out.println("Current Seller " + i);
    // here you are getting the elements each time you iterate, which will get the
    // latest element references
    driver.findElements(By.xpath("xpath of bestsellers")).get(i).click();
    clickOnAddToCartButton();
    driver.navigate().back();

}
Sign up to request clarification or add additional context in comments.

4 Comments

Tried the above code. Only 2 bestseller items are added to the cart when there are 4 bestsellers in the page. It is going to all the 4 bestseller products pages but only 2 are being added to the cart.
Try with ` driver.findElements(By.xpath("xpath of bestsellers")).get(0).click();` just replace i with 0 so that always the first item will be added.
No I want to add all the bestseller items in the first page.
After the 'add to cart' is clicked, 'go to cart' and 'Proceed to check out' options are displayed. I've waited for them to confirm the 'add to cart' is clicked otherwise I get to know the problem. It worked. I've updated the code in the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.