java - 登录过程: Not read my else statement in selenium

标签 java selenium if-statement selenium-webdriver

我的 for 循环中的 if else 。此 if else 用于验证来自 excel 的数据以进行登录。我尝试 bug,它显示它跳过 else 语句。

出现错误:

no such element " email". 

行错误: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));

                        }

最佳答案

将 if 更改为 check size避免异常

  driver.findElements( By.className("description") ).size() != 0

关于java - 登录过程: Not read my else statement in selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57601916/

相关文章:

javascript - Selenium(网络驱动程序)- 无法单击拆分按钮的右边缘(请参见图片链接)

selenium - 如何增加 session 超时(默认为 30 分钟)

javascript - 在 if 语句上访问数组

javascript - 多个 "if"语句?

java - 如何获取 javax.comm API?

java - 从只读文件夹启动时 Eclipse RCP 应用程序失败

selenium - Docker:尽管演示成功,但无法查看正在运行的容器

java - Neo4j Server 无法启动嵌入式数据库

java - 如何使用 Scheme/Lisp/Clojure 解决 Matrix/LP 问题?

bash - 如何制作 "if not true condition"?