Reputation: 175
As in the title - how to correctly refresh the "productList" to finally find the specific product in the catalogue?
this method clicks all the time in the button "Show Next Products" even if the product itself is visible on the page. It means that the "productList" hasn't been refreshed.
public void findProductAndAddToCart() {
//searching for products by className
List<WebElement> productList = SeleniumDriver.getDriver().findElements(By.className("bcom--txtBold"));
for (int i = 0; i < productList.size(); i++) {
//getting the numbers of the products
String element = productList.get(i).getText();
if (element.equals("7000029644")) {
productList.get(i).isDisplayed();
System.out.println("Product is displayed");
SeleniumDriver.getDriver().findElement(By.xpath("XPATH - Add to Cart Button')]")).click();
break;
} else {
//the product hasn't been found, so need to click "Show Next Products"
SeleniumDriver.getDriver().findElement(By.xpath("//a[contains(.,'Show Next')]")).click();
List<WebElement> newProductList = SeleniumDriver.getDriver().findElements(By.className("bcom--txtBold"));
productList.addAll(newProductList);
}
Upvotes: 0
Views: 803
Reputation: 108
You could try something like this
public void findProductAndAddToCart() {
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();
if (element.equals("7000029644")) {
e.isDisplayed();
System.out.println("Product is displayed");
SeleniumDriver.getDriver().findElement(By.xpath("XPATH1')]")).click();
flag = false;
break;
} else {
//don't click next here
}
}
SeleniumDriver.getDriver().findElement(By.xpath("//a[contains(.,'Show
Next')]")).click();
}
}
private boolean isNextEnabled() {
return SeleniumDriver.getDriver()
.findElements(By.xpath("//a[contains(.,'Show Next')]")).isEnabled;
}
Upvotes: 1