0

I am trying to validate a login section with all valid and invalid inputs.

here is the code i have tried:


    public void login_Valid_Invalid_Combinations() throws BiffException, IOException, InterruptedException 
    {
      String FilePath = "D://credentials.xls";
      FileInputStream FIS = new FileInputStream(FilePath);
      Workbook WB = Workbook.getWorkbook(FIS);
      Sheet SH = WB.getSheet(0);
      for(int row =0; row<= SH.getRows()-1; row++)
      {
          String userNAME = SH.getCell(0, row).getContents();
          String passWORD = SH.getCell(1, row).getContents();
          System.out.println("USERMANE : "+userNAME + "  PASSWORD : "+passWORD);
          driver.get("LOGIN PAGE URL");
          driver.findElement(By.id("Email")).sendKeys(userNAME);
          driver.findElement(By.id("Password")).sendKeys(passWORD);
          driver.findElement(By.id("btnlogin")).click();
          System.out.println(driver.findElement(By.xpath("//*[@id='eError']")).getText());
          Thread.sleep(2000);
          String URL = driver.getCurrentUrl();
          System.out.println(URL);
          if (URL.equals("URL AFTER SUCCESFULL LOGIN")) 
          {
              System.out.println("Login Successfull");
          }
          else 
          {
              System.out.println("Login Failed");
          }
      }
      driver.close();
    }

I want to show the error message each time login failed, which is working fine. But when login is successful with valid inputs, it's showing:

"org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='eError']"}"

as the error message is not coming for successful login.

Can you please help me how to handle this.

ATA
  • 51
  • 1
  • 10

3 Answers3

3

For such a case, i am not a fan of try/catch. You want to check if the element exists on the page and if it does print out the text it contains otherwise its a successful login,

//Use findElements
List<WebElement> elements = driver.findElements(By.xpath("//*[@id='eError']"));
if(elements.size() > 0)
{
    System.out.println(elements.get(0).getText())
}
Moe Ghafari
  • 2,227
  • 1
  • 12
  • 17
  • While this and the offered solution above both work, I think this one might be better in that it does not assume that any exception would be the NoSuchElementException, and therefore ignore *any* kind of exception. Rather than convolute the Try/Catch logic, this solution is cleaner. – Bill Hileman Mar 30 '17 at 19:43
  • I might shorten the code slightly as: 'if (driver.findElements(By.xpath("//*[@id='eError']")).size()>0) System.out.println(driver.findElements(By.xpath("//*[@id='eError']")).getText();' – Bill Hileman Mar 30 '17 at 19:48
  • Best practice according to the [Selenium docs](https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html#findElement-org.openqa.selenium.By-) is to use this method, `findElement should not be used to look for non-present elements, use findElements(By) and assert zero length response instead.` – JeffC Mar 30 '17 at 20:13
  • @BillHileman That doesn't really shorten the code and you have a typo in there because you are using `.findElements()`. Another issue with this method is that you end up scraping the page twice, which is not as efficient as the code posted. – JeffC Mar 30 '17 at 20:21
  • That's what I get for copy/paste. I only meant it was physically one line less (not counting the two braces) and I get your second point as well. These days (I'm an old dinosaur) computers are so fast and huge on storage, neither space nor time really matters, but old habits die slow. – Bill Hileman Mar 30 '17 at 20:28
2

You can replace the line

System.out.println(driver.findElement(By.xpath("//*[@id='eError']")).getText());

with this:

try {
    WebElement elem = driver.findElement(By.xpath("//*[@id='eError']"));
    System.out.println(elem.getText());
}
catch(Exception e) {
    // do nothing
}

The above code in try block finds the element with the xpath. If it is present, it goes on to next line to print the text. If the element is not present, the exception will be thrown and will be caught in the catch block and continue safely.

Jayesh Doolani
  • 1,233
  • 10
  • 13
0

In case of success you will not get error message, in this case below line will throw NoSuchElement Exception

System.out.println(driver.findElement(By.xpath("//*[@id='eError']")).getText());

We need to put above piece of code in try block then it will handles the exception.below is the updated code.

String userNAME = SH.getCell(0, row).getContents();
String passWORD = SH.getCell(1, row).getContents();
System.out.println("USERMANE : "+userNAME + "  PASSWORD : "+passWORD);
driver.get("LOGIN PAGE URL");
driver.findElement(By.id("Email")).sendKeys(userNAME);
driver.findElement(By.id("Password")).sendKeys(passWORD);
driver.findElement(By.id("btnlogin")).click();
Thread.sleep(2000);
String URL = driver.getCurrentUrl();
if (URL.equals("URL AFTER SUCCESFULL LOGIN")) 
  {
    System.out.println("Login Successfull");
  }
else 
  {
   System.out.println("Login Failed");
try{
   System.out.println(driver.findElement(By.xpath("//*[@id='eError']")).getText());
   }
catch(Exception e){
    }       
    }

Let me know if it helps you.

Akarsh
  • 967
  • 5
  • 9