title: How to solve this problem using if-else loop in java selenium?
body: <p>If variable value between j =&lt;10 then page through error &quot;Sorry ! user already...&quot;
  and then I try to assign another random value to variable k and fill up form again.</p>
  <p>I want to execute <code>driver.findElement(By.xpath(&quot;//*[contains(text(),'Sorry ! ')]&quot;)).isDisplayed()</code> step only if I get error. Else just print success message.</p>
  <p>Problem here is even if I get success message I'm getting element not found error due to <code>driver.findElement(By.xpath(&quot;//*[contains(text(),'Sorry ! ')]&quot;)).isDisplayed()</code>.
  I also tried implementing <code>boolean Sorry_Error = driver.findElement(By.xpath(&quot;//*[contains(text(),'Sorry ! ')]&quot;)).isDisplayed();</code> but I think it is similar logic just different way of implementation. if there is no error then loop should go for else block but insted it searching <code>.isDisplayed</code> xpath and throwing error.
  Isn't it correct way to handle user already exist error or is there any other logic.</p>
  <p><a href="https://i.stack.imgur.com/j2xVx.jpg" rel="nofollow noreferrer">This is success message</a></p>
  <p><a href="https://i.stack.imgur.com/1Qf9U.jpg" rel="nofollow noreferrer">This is error message</a></p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>package com.self.learn;
  
  import java.util.Random;
  import java.util.concurrent.TimeUnit;
  
  import org.openqa.selenium.By;
  import org.openqa.selenium.JavascriptExecutor;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.WebElement;
  import org.openqa.selenium.chrome.ChromeDriver;
  import org.openqa.selenium.support.ui.ExpectedConditions;
  import org.openqa.selenium.support.ui.WebDriverWait;
  import org.testng.annotations.Test;
  
  public class testandquiz {
  
      public static WebDriverWait wait;
  
      @Test
      public void Simple_Test() throws InterruptedException {
  
          System.setProperty("webdriver.chrome.silentOutput", "true");
          System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
          WebDriver driver = new ChromeDriver();
          wait = new WebDriverWait(driver, 10);
          driver.manage().deleteAllCookies();
  
          driver.get("https://www.testandquiz.com/user-registration");
          driver.manage().window().maximize();
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
          ((JavascriptExecutor) driver).executeScript("scroll(0,300)");
  
          WebElement Fname = driver.findElement(By.id("first_name"));
          WebElement Lname = driver.findElement(By.id("last_name"));
          WebElement User_name = driver.findElement(By.id("user_name"));
          WebElement email_address = driver.findElement(By.id("email_address"));
          WebElement password = driver.findElement(By.id("password"));
          WebElement re_password = driver.findElement(By.id("re_password"));
          WebElement city = driver.findElement(By.id("city"));
          WebElement state = driver.findElement(By.id("state"));
          WebElement country = driver.findElement(By.id("country"));
          WebElement pin_code = driver.findElement(By.id("pin_code"));
          WebElement contact_no = driver.findElement(By.id("contact_no"));
  
          Random randomGenerator = new Random();
          int j = 10;
          int k = 99999;
          int i = randomGenerator.nextInt(j);
  
          Fname.sendKeys("Test" + i + "");
          Lname.sendKeys("Test" + i + "");
          User_name.sendKeys("Test" + i + "");
          email_address.sendKeys("Test" + i + "");
          password.sendKeys("Test" + i + "");
          re_password.sendKeys("Test" + i + "");
          city.sendKeys("Test" + i + "");
          state.sendKeys("Test" + i + "");
          country.sendKeys("Test" + i + "");
          pin_code.sendKeys("Test" + i + "");
          contact_no.sendKeys("1234567890");
  
          driver.findElement(By.xpath("//button[contains(text(),'Sign Up')]")).click();
  
          if (driver.findElement(By.xpath("//*[contains(text(),'Sorry ! ')]")).isDisplayed()) {
  
              WebElement Fname1 = driver.findElement(By.id("first_name"));
              WebElement Lname1 = driver.findElement(By.id("last_name"));
              WebElement User_name1 = driver.findElement(By.id("user_name"));
              WebElement email_address1 = driver.findElement(By.id("email_address"));
              WebElement password1 = driver.findElement(By.id("password"));
              WebElement re_password1 = driver.findElement(By.id("re_password"));
              WebElement city1 = driver.findElement(By.id("city"));
              WebElement state1 = driver.findElement(By.id("state"));
              WebElement country1 = driver.findElement(By.id("country"));
              WebElement pin_code1 = driver.findElement(By.id("pin_code"));
              WebElement contact_no1 = driver.findElement(By.id("contact_no"));
  
              Fname1.clear();
              Fname1.sendKeys("Test" + k + "");
              Lname1.clear();
              Lname1.sendKeys("Test" + k + "");
              User_name1.clear();
              User_name1.sendKeys("Test" + k + "");
              email_address1.clear();
              email_address1.sendKeys("Test" + k + "");
              password1.clear();
              password1.sendKeys("Test" + k + "");
              re_password1.clear();
              re_password1.sendKeys("Test" + k + "");
              city1.clear();
              city1.sendKeys("Test" + k + "");
              state1.clear();
              state1.sendKeys("Test" + k + "");
              country1.clear();
              country1.sendKeys("Test" + k + "");
              pin_code1.clear();
              pin_code1.sendKeys("Test" + k + "");
              contact_no1.clear();
              contact_no1.sendKeys("1234567890");
  
              driver.findElement(By.xpath("//button[contains(text(),'Sign Up')]")).click();
          }
  
          else {
  
              String Message = driver.findElement(By.xpath("//div[contains(text(),'verification mail')]")).getText();
              System.out.println(Message);
          }
  
      }
  }</code></pre>
  </div>
  </div>
  </p>
  <p>I don't know how to implement this logic.</p>