-1

I have a scenario, where page takes me to a screen where it has a message like the functionality is not enabled. hence you are here. Then i need to navigate to the settings page and has to enable it & reload the form to verify the web elements.

Initially everything was fine, second time when i ran the code. i have encountered no such element exception. because the setting was disabled initially, which has been enabled through the automated script and it is enabled now and i can see the form elements. I have to execute the system setting code only when the element is found else i have to skip the code.

I tried below way, but i still get no such element found error

if(driver.findElement(By.id("ViewErrorMessage")) != null) {

      String errorMsg = driver.findElement(By.xpath("//*[@id=\"ctl00_pnlViewErrorMessage\"]")).getText();
  Assert.assertEquals(errorMsg, "Form is disabled, Hence you are here. Enable the form");

      //system setting code

      driver.findElement(By.xpath("//input[@name='btnSignin']")).click();
      Thread.sleep(2000);
      driver.findElement(By.xpath("//img[@id='imgLogo']")).isDisplayed();
}else{

  //code to verify form elements goes here..
}

error is reported at the if condition. when element is not found, i am assuming it has to execute else loop but it always fails at the same point. could anyone please help.

frianH
  • 7,295
  • 6
  • 20
  • 45
Neelima
  • 1
  • 2

2 Answers2

0

When driver is not able to find the element it throws NoSuchElementFound exception. You can either wrap the findElement in a try catch to see if the element exists or use isEmpty method.

boolean isElementPresent=true;
try
{
   driver.findElement(By.id("ViewErrorMessage"));
}
catch
{
  isElementPresent=false;
}

if(isElementPresent) {

      String errorMsg = driver.findElement(By.xpath("//*[@id=\"ctl00_pnlViewErrorMessage\"]")).getText();
  Assert.assertEquals(errorMsg, "Form is disabled, Hence you are here. Enable the form");

      //system setting code

      driver.findElement(By.xpath("//input[@name='btnSignin']")).click();
      Thread.sleep(2000);
      driver.findElement(By.xpath("//img[@id='imgLogo']")).isDisplayed();
}else{

  //code to verify form elements goes here..
}

Or you could do like

if(!driver.findElements(By.id("ViewErrorMessage")).isEmpty()) {

      String errorMsg = driver.findElement(By.xpath("//*[@id=\"ctl00_pnlViewErrorMessage\"]")).getText();
  Assert.assertEquals(errorMsg, "Form is disabled, Hence you are here. Enable the form");

      //system setting code

      driver.findElement(By.xpath("//input[@name='btnSignin']")).click();
      Thread.sleep(2000);
      driver.findElement(By.xpath("//img[@id='imgLogo']")).isDisplayed();
}else{

  //code to verify form elements goes here..
}
Amith YR
  • 172
  • 10
  • Option1 worked for me, Thanks. option2 didn't work though. isEmpty method was not listed in the available methods for a webelement – Neelima Nov 27 '19 at 00:42
0

Please try isDisplayed in if contion, hope this will help:

if(driver.findElement(By.id("ViewErrorMessage").isDisplayed()) {

      String errorMsg = driver.findElement(By.xpath("//*[@id=\"ctl00_pnlViewErrorMessage\"]")).getText();
  Assert.assertEquals(errorMsg, "Form is disabled, Hence you are here. Enable the form");

      //system setting code

      driver.findElement(By.xpath("//input[@name='btnSignin']")).click();
      Thread.sleep(2000);
      driver.findElement(By.xpath("//img[@id='imgLogo']")).isDisplayed();
}else{

  //code to verify form elements goes here..
}

or Use below

String s1 = "Error message";
String s2 = driver.findElement(By.id("ViewErrorMessage").getText());
if(s1 == s2) {

          String errorMsg = driver.findElement(By.xpath("//*[@id=\"ctl00_pnlViewErrorMessage\"]")).getText();
      Assert.assertEquals(errorMsg, "Form is disabled, Hence you are here. Enable the form");

          //system setting code

          driver.findElement(By.xpath("//input[@name='btnSignin']")).click();
          Thread.sleep(2000);
          driver.findElement(By.xpath("//img[@id='imgLogo']")).isDisplayed();
    }else{

      //code to verify form elements goes here..
    }
  • Thanks for your time Mahesh, i tried both the ways but the error still exists: nosuchelementexception – Neelima Nov 27 '19 at 00:49