首页 > 解决方案 > 验证购物车中可用的产品,然后使用删除链接 a 使用 selenium Java 删除。这里的产品数量未知

问题描述

java中的selenium脚本检查购物车中可用的产品,如果可用,则使用删除链接删除。

for(int i = result; i >= 0;  i--) {

   boolean str = "Product available with remove link";

   if(str) {
      // Click on remove link till remove link there to remove all product 
   }  
   else {
      // Verify no product present Text 
   }
}

这里的问题 Loop 只运行一次并且只删除一个产品。每个产品都有相关的删除链接。我们不知道购物车中列出的产品数量。所以我们必须删除所有这些。如果不可用,则显示消息产品未列出。

标签: javaseleniumfor-loopconditionalbase

解决方案


if.

public void removeProducts() {
    List<WebElement> removeProductBtnList = driver.findElements(locator);
    for (WebElement removeProductBtn : removeProductBtnList ) { 
        driver.findElement(locator).click();
        //removeProductBtn.click();  you can also use this line instead of the above, 
        //but it will probably return a StaleElementReferenceException after the first click.
    }
}

如果购物车上没有 removeButtons,则此方法不执行任何操作。


推荐阅读