超时异常或没有此类元素异常

时间:2014-11-05 10:51:09

标签: selenium-webdriver

 @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:\用户\穆拉利\应用程序数据\本地\ TEMP \ TestNG的-蚀-609099432 \ TestNG的-customsuite.xml

     

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

     

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

     

显示上述错误。如果我使用driver.findElement它会显示No   元素异常。如果我用户   wait.until(ExpectedCondition.presenceOfElementLocated),它显示   TimeoutException异常。我怎么能解决这个问题

1 个答案:

答案 0 :(得分:1)

wait.until()将抛出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());
}