Problem Statement:
Suppose there are requirements that you have created some function to fetch data from a table. And In the table, you have data in some fields; in some, it’s not.
And The locator to fetch data is the same. But now it will take a very long time to bring the data in this case because your script will check for every element that exists or not. Suppose some values are not present and you are using try catch to continue the execution.
In this case, if 10 values are empty, then the time to fetch data will increase.
10*10 = 100 extra seconds.
Now, suppose the implicit wait is 10 seconds, the total wait to fetch data will be very high.
WebDriver driver = new ChromeDriver(); // Initialize your WebDriver
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Set the original implicit wait value (e.g., 10 seconds)
Solution
Identify the element for which you want to skip the implicit wait:
WebElement elementToSkipWait = driver.findElement(By.id("elementId")); // Replace "elementId" with the actual ID of the element
Temporarily set the implicit wait to zero:
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
Act on the element without waiting:
elementToSkipWait.click(); // Perform the desired action on the element
Restore the original implicit wait value:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Set it back to the original value (e.g., 10 seconds)
By setting the implicit wait to zero before acting on a specific element, Selenium will not wait for the element to appear, and the action will be performed immediately. However, it’s essential to restore the original implicit wait value to avoid unexpected behaviour in the subsequent steps.
Keep in mind that skipping the implicit wait should be used judiciously, as it may lead to flaky tests if the elements are not ready to interact when the action is executed. In general, it’s better to use explicit waits or other synchronization methods to ensure that your test scripts are robust and reliable.