0

I have an input field that accepts numbers. I need to enter numbers and check if the error message is displayed or not. The error message shows up when an invalid number is entered. I also need to check if the error message shows up when I enter a valid number but I get NoSuchElementException exception. The below HTML code is when an error message not displayed.

<div class="date text_feild_last">
  <input type="text" id="seqNo" class="form-control" name="seq_num" value="1" placeholder="Enter Number" autocomplete="off" data-orig="921">
  <div hidden id="seq-num-error" class="help-inline"></div>
</div>

The HTML code when an error message displayed is as below

<div class="date text_feild_last">
    <input type="text" id="seqNo" class="form-control" name="seq_num" value="1" placeholder="Enter Number" autocomplete="off" data-orig="921">
    <div hidden id="seq-num-error" class="help-inline" style="display: block;">Please enter valid number</div>
</div>

The xpath I am using to find the error message is WebElemet errorMessage = driver.findElement(By.xpath("//div[contains(text(), 'Please enter valid number')]"));

I am checking if the element is displayed and then doing my next operation but not able to carry out the next operation as I am getting an exception. Please help me. Thank you.

Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26
Kitty Raj
  • 91
  • 1
  • 2
  • 12
  • does this answer your question? https://stackoverflow.com/questions/2646195/how-to-check-if-an-element-is-visible-with-webdriver – verity Dec 19 '20 at 11:18

1 Answers1

1

So basically the error message is not gonna show up in the tag or screen if you enter a valid number. Selenium will try to find it on the screen and will throw exception.

To handle this, surround the line with try and catch. Catch the exception and log it as Pass for valid numbers or log it as Fail for invalid numbers and continue with the program

try {
WebElement errorMessage = driver.findElement(By.xpath("//div[contains(text(), 'Please enter valid number')]"));
} catch (NoSuchElementException e){
//Log the message and continue
Dev
  • 2,739
  • 2
  • 21
  • 34