After looking at all the answers and comments here, I am summarizing them with some code to test the simultaneous use of both implicit and explicit waits.
Use implicit waits only when you (generally) don't need to check for absence of elements, for example in a throw away web scraping project.
Never mix implicit and explicit waits together. Refer link1 and link2. If you test for absence of an element, then wait time becomes unpredictable. In the below code, only sometimes the wait time = implicit wait. You can test for absence by simply using an invalid locator.
I have taken the code in link2 and refactored it make it short and provide a summary. The code shows the actual wait time when both implicit and explicit waits are used.
The code below goes to a website and tries to find a valid element and invalid element. It uses both implicit and explicit waits. In case of invalid element search, it tries different combinations of implicit/IW and explicit/EW wait times - IW = EW, IW > EW and IW < EW.
First, the output :
WHEN ELEMENT IS FOUND WITHOUT ANY DELAY :
>>> WITH implicit = 30, explicit = 20 ::::: Wait time = 0
WHEN ELEMENT IS NOT FOUND :
a. When implicit wait = explicit wait.
>>> WITH implicit = 10, explicit = 10 ::::: Wait time = 10. ***WITH EXCEPTION*** : NoSuchElementException
b. When implicit wait > explicit wait.
>>> WITH implicit = 30, explicit = 10 ::::: Wait time = 30. ***WITH EXCEPTION*** : NoSuchElementException
c. When implicit wait < explicit wait.
>>> WITH implicit = 10, explicit = 30 ::::: Wait time = 10. ***WITH EXCEPTION*** : NoSuchElementException
The code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
/*
* Facing this chromedriver error after opening browser - [SEVERE]: Timed out receiving message
* from renderer: 0.100.
* */
public class TimeTest {
static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd-M-yyyy hh:mm:ss a");
static final String URL = "https://www.redbus.in/";
static final String TIME_ZONE_NAME = "Europe/Madrid";
static final By validLoc = By.id("src");
static final By inValidLoc = By.id("invalid locator");
static WebDriver driver;
public static void main(String[] args) {
dateFormat.setTimeZone(TimeZone.getTimeZone(TIME_ZONE_NAME));
//>>> Open chrome browser
System.setProperty("webdriver.chrome.driver", "C:/drivers/chromedriver.exe");
TimeTest.driver= new ChromeDriver();
driver.manage().window().maximize();
//>>> Test waiting logic.
System.out.println("\n\nWHEN ELEMENT IS FOUND WITHOUT ANY DELAY : ");
//mixing of implicit wait and explicit wait will not impact on webdriver behavior.
testWait(30, 20, validLoc, "");
System.out.println("\n\nWHEN ELEMENT IS NOT FOUND : ");
//Run the method multiple times. Wait time generally = 10 seconds, but sometimes = 20 seconds.
testWait(10, 10, inValidLoc, "a. When implicit wait = explicit wait.");
//Wait time always = implicit wait. Generally ?
testWait(30, 10, inValidLoc, "b. When implicit wait > explicit wait.");
//Wait time always = implicit wait. Generally ?
testWait(10, 30, inValidLoc, "c. When implicit wait < explicit wait.");
//>>> Close the browser.
driver.quit();
}
public static void testWait(int implicitWait, int explicitWait, By locator, String comment){
// setting implicit time
driver.manage().timeouts().implicitlyWait(implicitWait, TimeUnit.SECONDS);
// Loading a URL
driver.get(URL);
// defining explicit wait
WebDriverWait wait= new WebDriverWait(driver, explicitWait);
// Locating and typing in From text box.
Date start = new Date();
String waitStats = comment + "\n>>> WITH implicit = " + implicitWait + ", explicit = " + explicitWait +
" ::::: " ;//+ "Wait start = " + dateFormat.format(start)
String exceptionMsg = "";
try {
WebElement fromTextBox = wait.until(ExpectedConditions.visibilityOf(driver.findElement(locator)));
}catch (Exception ex){
exceptionMsg = ". ***WITH EXCEPTION*** : " + ex.getClass().getSimpleName();
}
Date end = new Date();
//waitStats += ", Wait end = " + dateFormat.format(end)
waitStats += "Wait time = " +
TimeUnit.SECONDS.convert(end.getTime() - start.getTime(), TimeUnit.MILLISECONDS)
+ exceptionMsg + "\n";
System.out.println(waitStats);
}
}
NoSuchElementException
? It could fail with aWebDriverException
,StaleElementReferenceException
,ElementNotVisibleException
or something similar. I'll try to guess the first one.Implicit wait
waits only until the function firesNoSuchElementException
s... – Backbone