1

My if else inside my for loop. This if else to verify the data from the excel to login. I try to bug, it shows that it skip else statement.

Getting error :

no such element " email". 

The error at line: driver.findElement(By.name("email")).clear();

    for(int i=1; i<=5; i++) { 
                        cell = sheet.getRow(i).getCell(0);
                        WebElement email = driver.findElement(By.name("email"));
                        email.sendKeys(cell.getStringCellValue());                      

                        cell = sheet.getRow(i).getCell(1);
                        WebElement password = driver.findElement (By.name("password"));
                        password.sendKeys(cell.getStringCellValue());


                        driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/form/div[4]")).click();
                        Thread.sleep(8000);

                        try {

                            if(driver.findElement(By.className("description")).isDisplayed()) {
                                System.out.println("invalid");
                                driver.findElement(By.id("btn-skip")).click();

                                }
                                else{
                                        System.out.println("invalid");
                                        //driver.findElement(By.id("btn-skip")).click();
                                }

                        }
                        catch(NoSuchElementException e) {

                        }

                        driver.findElement(By.name("email")).clear();

                        System.out.print(sheet.getRow(i).getCell(0) + " ");
                        System.out.println(sheet.getRow(i).getCell(1));

                        }

user7294900
  • 52,490
  • 20
  • 92
  • 189
orkeddelilah
  • 63
  • 1
  • 8

2 Answers2

1

Change your if to check size to avoid exception

  driver.findElements( By.className("description") ).size() != 0
user7294900
  • 52,490
  • 20
  • 92
  • 189
  • @samazlan see differences https://www.softwaretestingmaterial.com/difference-between-findelement-and-findelements-methods/ – user7294900 Aug 22 '19 at 06:17
0

Try to check whether there aren't more elements with the same class name 'Description'. If there are, you could use CssSelector to narrow it down to the one you want.

RegCent
  • 88
  • 10