TimeOutException in Selenium

In Selenium, TimeOut exception occurs when a command takes longer than the wait time to avoid the ElementNotVisible Exception.

If the commands do not complete even after the wait time is over, a TimeOut Exception is thrown.

svg viewer

Solution

  1. You can manually increase the wait time by hit-and-trial. If the problem persists for a longer period of time, there may be some other issue and you should continue onto the next solution.

  2. You can explicitly add wait by using JavaScript Executor. This will wait until the function returns “complete” and then continue with the code.

//add webdriver wait for 30 seconds
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
 
//wait until the page return complete as its status
wait.until(webDriver -> ((JavascriptExecutor)webDriver).executeScript("return document.readyState").equals("complete"));

//fetch the webpage
driver.get("educative.io");
  1. There may be a chance that there are multiple inputs present with the same id. If one of them is hidden, and Selenium is interacting with that element, then there is a chance that Selenium will not be able to return it.
    You can try using some other property to locate the element such as CSS Selector or Xpath.

  2. Use implicit waits. This will ensure all timeouts happen after the given time. This should be declared at the start of the program before carrying out any tasks.

driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
Copyright ©2024 Educative, Inc. All rights reserved