Occasionally we encounter the NoSuchElementException when automating a web application, which is thrown when the requested element is not found when we test the website with Selenium. The primary reason for this error is that the element that requires interaction is present on the page; however, it takes some time for it to load and appear for the user. It’s clear that this could become a serious issue during automation and result in our scripts failing. Here is where Thread.sleep() or implicit wait methods in Selenium Java come into play. Let us explore the fundamental principles of the Thread.sleep() and Implicit wait methods by utilizing examples.
Before we get into the methods’ discussions, lets take a look at the reasons we need them.
Reasons to use Thread.sleep and implicit methods:
There are several reasons why wait commands are necessary in Selenium.
- Page loading: Web pages can take different amounts of time to load, depending on how fast the network is, how quickly the server responds, and other things. If you attempt to interact with elements on the page prior to the page has fully loaded, your script may encounter failure.
- Dynamic content: Many modern web pages use dynamic content that is loaded asynchronously, which means that it can take time for certain elements to become available to users. If you attempt to interact with these elements prior to their availability, your script may encounter failure.
- Latency: Network latency can cause delays in the availability of elements on a page. If you do not utilize wait commands, your script may encounter failure if it attempts to interact with elements prior to their complete loading.
- Animations: Some websites have animations or other visual effects that can make things take longer to load or when things happen on the page. If you don’t use wait commands, your program might fail if it tries to interact with things during these animations.
Also check video Differences of Thread.sleep & Implicit wait
What is Thread.sleep()?
Selenium employs the sleep wait command to suspend execution for a specific circumstance or duration.
It halts the execution and enables us to gain insight into the events that transpired during the interruption. It will accept a time specification based on milliseconds. This method is extremely useful for debugging a website or web page.
Example
package selPaackadsf; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class ThreadExample{ public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver","C:\\Program Files\\chromedriver.exe"); ChromeDriver driver= new ChromeDriver(); driver.get("http://demo.itlearn360.com/"); driver.manage().window().maximize(); driver.findElement(By.id("loginlabel")).click(); Thread.sleep(5000); driver.findElement(By.name("log")).sendKeys("Demo12"); Thread.sleep(5000); driver.findElement(By.name("pwd")).sendKeys("Test123456$"); driver.findElement(By.name("wp-submit")).click(); } }
What is Implicit Wait?
The implicit wait directive instructs the driver to wait for a specified duration before generating an exception. As we know, in Selenium, the default time for executing code is between 0 and 3 seconds. In some cases, the elements may require additional time to execute than the default time. Implicit wait increases test script execution time and provides a certain time period for element to locate.
Example:
package selPaackadsf; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class ImplicitWaitClass{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\\Program Files\\chromedriver.exe"); ChromeDriver driver= new ChromeDriver(); driver.get("http://demo.itlearn360.com/"); driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS); driver.manage().window().maximize(); driver.findElement(By.id("loginlabel")).click(); driver.findElement(By.name("log")).sendKeys("Demo12"); driver.findElement(By.name("pwd")).sendKeys("Test123456$"); driver.findElement(By.name("wp-submit")).click(); } }
Difference between in Thread.sleep & Implicit wait
Implicit Waits | time.sleep() |
If an element is found before a certain time, then the process moves on to the next step. | If the element is not present within the specified time frame, the flow will continue uninterrupted throughout the duration. |
The process is more efficient as the flow does not pause further once the element is located. | It is slower because it waits for a certain amount of time to finish, even if it finds the element. |
It’s better than using time.sleep() | It is always preferred to use explicit waits in situations where there is an option to choose between time.sleep() and explicit waits. |