0

I'm in product catalogue in e-commerce store and I would like to choose one particular product with number "7000029644". On the displayed page, there are 8 products available, so I need to click the "Show Next Products" button to see this "7000029644" which is on the second page.

I've created the loop below, but the behaviour is like that (also, attached the displayed output from the console): 1) I get all the webelements with className "bcom-txtBold" 2) getText of the one element and if it is not found, then the driver clicks "Show Next Products" button 3) I get all the webelements again 4) driver shows the text of the 2nd webelement

The issue is that every iteration of the element - the driver clicks the button. So if I want to find the product which is on the 14th position - the driver clicks 14 times the button, which is not necessary because I need to click only once, because the product is on the 2nd page displayed.

 public void findProductAndAddToCart() throws InterruptedException {

   boolean flag = true;

    while (flag) {
        flag = isNextEnabled();

        //searching for products by className
        List<WebElement> productList = SeleniumDriver.getDriver()
                .findElements(By.className("bcom--txtBold"));

        for (WebElement e : productList) {

            //getting the numbers of the products
            String element = e.getText();
            System.out.println("Element ------->>>> " + element);

            if (element.equals("7000029644") {
                e.isDisplayed();
                System.out.println("Product is displayed");
                Thread.sleep(10000);
                galleryPageLocators.galleryBlock.click();
                galleryPageLocators.addToCartBtn.click();
                flag = false;
                break;

            } else {
                Thread.sleep(5000);
                //the product hasn't been found, so need to click "Show Next Products"
                SeleniumDriver.getDriver().findElement(By.xpath("//a[contains(.,'Show Next')]")).click();
                System.out.println("Product List  ------>>>>>>>" + productList);

            }
        }
    }

}

private boolean isNextEnabled() {
    return SeleniumDriver.getDriver()
            .findElement(By.xpath("//a[contains(.,'Show Next')]")).isEnabled();

}

enter image description here

0

You have to iterate through the whole WebElement product list before you click on 'Next page' button. What you are doing is get the first element check whether if it is the product and if it is not the product, you click on 'Next page' button (execute else block).

  1. When you get a product list, iterate through the whole list
  2. If the product is not found, click on 'Next page' button (If the product is found, break the outer loop which is while loop)
  3. Wait till all products loaded in the 2nd page
  4. Then get the new product list to a WebElement list
  5. Then look for the specific product again.

So the code should be;

    OUTER:
    while (seekingProduct) {
        seekingProduct = isNextEnabled();

        //searching for products by className
        List<WebElement> productList = SeleniumDriver.getDriver()
            .findElements(By.className("bcom--txtBold"));

        for (WebElement e : productList) {

            //getting the numbers of the products
            String element = e.getText();
            System.out.println("Element ------->>>> " + element);

            if (element.equals("7000029644") {
                e.isDisplayed();
                System.out.println("Product is displayed");
                Thread.sleep(10000);
                galleryPageLocators.galleryBlock.click();
                galleryPageLocators.addToCartBtn.click();
                seekingProduct = false;
                break OUTER;
            }
         }
         if (seekingProduct) {
             // If the code reach here; it means product is not found in the page. Therefore click on next page button
             SeleniumDriver.getDriver()
                .findElement(By.xpath("//a[contains(.,'Show Next')]")).click();
        }
     }
| improve this answer | |
  • Thank you very much! That code resolved the issue. I am beginner and I didn't have the idea how to do it :) Have a great day! – Beti May 12 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.