如何使用java/selenium在for循环中添加条件

如何使用java/selenium在for循环中添加条件,java,selenium,Java,Selenium,我想点击添加按钮添加项目,点击添加按钮后,如果它显示错误,那么它应该删除添加的项目,再次点击添加按钮添加项目 这是我的密码: driver.findElement(By.id("addItem")).click(); // it will add item WebElement errrmsg = driver.findElement(By.id("sameAMC-invalid")); /* if error is displayed then it shou

我想点击添加按钮添加项目,点击添加按钮后,如果它显示错误,那么它应该删除添加的项目,再次点击添加按钮添加项目

这是我的密码:

    driver.findElement(By.id("addItem")).click(); // it will add item 

    WebElement errrmsg = driver.findElement(By.id("sameAMC-invalid"));


     /* if error is displayed then it should remove added item and should
      *again add another item again clicking on add item button and should again check
      *whether it is showing error message or not if not then else condition should get
      *execute*/

    if(errrmsg.isDisplayed()) 
    {
        driver.findElement(By.id("remove-Itemadd")).click();
        Thread.sleep(2000);
    }
    else
    {
        driver.findElement(By.id("button-payment")).click(); 
        Thread.sleep(6000);

    }

我想在发生错误时设置循环条件,即它应该再次单击“添加项”按钮,并且它应该检查是否显示任何错误,如果没有,那么它应该在else条件下执行。。。。。我不知道该怎么做……需要帮助吗?您可以使用,如下所示。但它进入无限循环,如果你知道最大计数。我们还可以包括要检查的条件

driver.findElement(By.id("addItem")).click(); // it will add item 

WebElement errrmsg = driver.findElement(By.id("sameAMC-invalid"));

while(errrmsg.isDisplayed()) 
{
    driver.findElement(By.id("remove-Itemadd")).click();
    Thread.sleep(2000);
    driver.findElement(By.id("addItem")).click(); // it will add item
    errrmsg = driver.findElement(By.id("sameAMC-invalid")); 
}

driver.findElement(By.id("button-payment")).click(); 
Thread.sleep(6000);

尝试以下代码:使用findElement而不是findElement

     driver.findElement(By.id("addItem")).click();
     while(true) {
        List<WebElement> errMsg =   UI.getdriver().findElements(By.id("sameAMC-invalid"));
        if(errMsg.size()==1) {
                driver.findElement(By.id("remove-Itemadd")).click();
                Thread.sleep(2000);
                driver.findElement(By.id("addItem")).click(); 
        }
        else if(errMsg.size() == 0 ) {
            driver.findElement(By.id("button-payment")).click(); 
            Thread.sleep(6000);
            break;
        }
    }  
driver.findElement(By.id(“addItem”))。单击();
while(true){
List errMsg=UI.getdriver().findElements(By.id(“sameAMC无效”);
如果(errMsg.size()==1){
driver.findElement(By.id(“删除项添加”))。单击();
《睡眠》(2000年);
driver.findElement(By.id(“addItem”))。单击();
}
else if(errMsg.size()==0){
driver.findElement(按.id(“按钮付款”))。单击();
睡眠(6000);
打破
}
}  
注意:@Murthi提供的代码将失败,如果没有错误消息。

您可以尝试下一步:

private By removeButtons = By.xpath("remove-Itemadd");
private By addButtons = By.xpath("addItem");
private By errorMsg = By.xpath("sameAMC-invalid");
private By payButton = By.id("button-payment");

private boolean isErrorOccur() {
    try {
        return driver.findElement(errorMsg).isDisplayed();
    } catch (org.openqa.selenium.NoSuchElementException ex) {
        return false;
    }
}

public void someMethod() {
    List<WebElement> addItemsButtons = driver.findElements(addButtons);
    List<WebElement> deleteItemsButtons = driver.findElements(removeButtons);

    //it will be works if this add/remove buttons are enabled for all items - list size is the same
    for (int i = 0; i < addItemsButtons.size(); i++) {
        addItemsButtons.get(i).click();
        if (isErrorOccur()){
            deleteItemsButtons.get(i).click();
            //here need some wait, possibly
        }
    }
    driver.findElement(payButton).click();
    //here need some wait, possibly 

}
private By removeButtons=By.xpath(“remove Itemadd”);
专用于addButtons=By.xpath(“addItem”);
private By errorMsg=By.xpath(“sameAMC无效”);
私人按payButton=按.id(“按钮付款”);
私有布尔值(){
试一试{
返回driver.findElement(errorMsg).isDisplayed();
}catch(org.openqa.selenium.NoSuchElementException ex){
返回false;
}
}
公共方法(){
列表addItemsButtons=driver.findElements(addButtons);
List deleteItemsButtons=driver.findelelements(removeButtons);
//如果所有项目都启用了此添加/删除按钮,则该功能将正常工作-列表大小相同
对于(int i=0;i
当错误不存在时,您是否收到NoTouchElement异常?您将收到多少次错误警报?如果你把它放在循环中,它可能会进入无限循环。这样可以吗?那么我们也可以使用重新创建方法。如果发生持续错误警报,它可能会导致无限循环。所以测试用例可能不会失败并通过。@PrachiDalvi:你能告诉我们这个问题的状态吗?