提问者:小点点

超时异常或无该元素异常


 @Test
      public void TC8() {

          driver.findElement(By.id("id_username")).sendKeys("admin");
          driver.findElement(By.id("id_password")).sendKeys("admin");
          driver.findElement(By.cssSelector("button,input[type='button']")).click();
          Reporter.log("TC101 > Login successfully to crossfraudet");
      }

     @Test
     public void TC9() {

         Assert.assertEquals("USER PROFILE", "USER PROFILE");
         Assert.assertEquals("SETTINGS", "SETTINGS");
         Assert.assertEquals("DASHBOARD", "DASHBOARD");
         Assert.assertEquals("ADMINISTRATION", "ADMINISTRATION");
     }

      @Test
      public void TC10() {

        wait=new WebDriverWait(driver,30);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".icon-users"))).click();
    wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#menuList > li.dropdown.open > ul > li:nth-child(3) > a"))).click();
    Reporter.log("TC10 > Click on User Profile > Profiles");   
      }

[TestNG]运行:
C:\用户\Murali\AppData\本地\Temp\testng-eclipse-609099432\testng-自定义套件. xml

通过:TC8通过:TC9
失败:TC10

org. openqa.selenium.TimeoutException:等待By.selector:.icon用户定位的元素可见性30秒后超时构建信息:版本:“2.43.1”,修订版:“5163bce”,时间:“2014-09-10 16:27:33”

它显示了上述错误。如果我使用river. findElement,它会显示No这样的元素异常。如果我的用户wait.到(期望的条件.presenceOfElement定位),它会显示timeout异常。我如何解决这个问题


共1个答案

匿名用户

即使元素不存在,wait.到()也会抛出TimeoutException。但是如果您尝试捕获异常并打印其原因,您将能够看到异常的确切原因。它可能是org. openqa.selenium.NoSuchElementException。请尝试以下代码:

WebDriverWait wait=new WebDriverWait(driver,30);
try {
       wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".icon-users"))).click();
} catch (TimeoutException exception) {
    System.out.println(exception.getCause().toString());
}