title: Selenium: how to write if condition when there is no element for some xpath
body: <p>Below is the code and question:</p>
  
  <pre><code>if(result.findElement(By.xpath(".//*[@class='s-item-container']/div[4]/span/a/i")).getText().equals("Lightning Deal"))
  </code></pre>
  
  <p>After we search any product on Amazon, we can also see the "Lighting Deal" option on some products. Now here I am trying to check if the lighting deal option is present or not. If it is present then condition is true and everything is okay. But if there is no lighting deal, then this gives the error <code>NoSuchElementException</code> because then <code>div[4]/span/a/i</code> doesn't exist. This <code>div[4]/span/a/i</code> comes into life only when there is some lighting deal.</p>
  
  <p>Please suggest me how to write this if condition.</p>
  
  <p>Complete Code:</p>
  
  <pre><code>//All the products after searching in List
  List&lt;WebElement&gt; resultsList = driver.findElements(By.xpath(".//*[starts-with(@id, 'result_')]"));
  
  for(WebElement result:resultsList)
  {
  System.out.println("Name of the product:"+result.findElement(By.tagName("h2")).getText());
  
  System.out.println("Brand: "+result.findElement(By.xpath(".//*[@class='s-item-container']/div[3]/div[2]/span[2]")).getText());
  
      if(result.findElement(By.xpath(".//*[@class='s-item-container']/div[4]/span/a/i")).getText().equals("Lightning Deal"))
      {
          String sp =result.findElement(By.xpath(".//*[@class='s-item-container']/div[5]/div[1]/a/span[2]")).getText();
          System.out.println("Selling Price: "+sp);       
      }
  
      else
      {
          System.out.println("Selling Price:"+result.findElement(By.xpath(".//*[@class='s-item-container']/div[4]/div[1]/a/span[2]")).getText());
      }
  }
  </code></pre>