Exception handling: Either a timeout occurred or the element could not be found

 @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] Running:
C:\Users\Murali\AppData\Local\Temp\testng-eclipse-609099432\testng-customsuite.xml

PASSED: TC8 PASSED: TC9
FAILED: TC10

org.openqa.selenium.TimeoutException: Timed out after 30 seconds waiting for visibility of element located by By.selector: .icon-users Build info: version: '2.43.1', revision: '5163bce', time: '2014-09-10 16:27:33'

An error is displayed indicating a Timeout Exception when trying to locate the element with class "icon-users". Using different approaches like driver.findElement or wait.until(ExpectedCondition.presenceOfElementLocated) results in No such element exception or timeoutexception respectively. How can this issue be resolved?

Answer №1

By utilizing the <code>wait.until()
method, a TimeoutException may be thrown even if the element is not found. However, by catching the exception and displaying its cause, you can pinpoint the exact reason for the issue. This could potentially be an
org.openqa.selenium.NoSuchElementException.
For a demonstration, consider running the following code:

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());
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the most efficient method for validating an exception in Protractor?

In recent code reviews within our team, a discussion arose about writing tests that assert on expected exceptions. While this is correct, the method used involved a try/catch block, which some believe may not be the most performant approach. This raises th ...