title: How to put add condition in for loop using java / selenium
body: <p>I wanted to click on Add button to add items  , after clicking on add button if it displayed error then it should remove that added item and again it should click on add button to add items</p>
  
  <p>Here is my code:</p>
  
  <pre><code>    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);
  
      }
  </code></pre>
  
  <p>I want to put loop condition when error occured , which is it should again click on add item button and also it should check whether it is showing any error if not then it should execute in else condition ..... i don't know how to do this ...need help </p>