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();
}