I automated routine works on a specific web page by selenium, then I faced a problem which I couldn’t get a required result. Because I wanted to receive the results from the Web API, I had to wait until the results were returned.
Perhaps the most common challenge for browser automation is ensuring that the web application is in a state to execute a particular Selenium command as desired. The processes often end up in a race condition where sometimes the browser gets into the right state first(things work as intended) and sometimes the Selenium code executes first(things do not work as intended). This is one of the primary causes of flaky tests.
Selenium, Waiting Strategies
There are 2 kinds of waiting methods such as Implicit Wait and Explicit Wait.
I will explain it.
Implicit Wait
This is a global setting that applies to every element location call for the entire session.
Selenium, Waiting Strategies;
Implicit Wait enable to set waiting time for entire life of the WebDriver instance, while Explicit Wait only sets the wait time for a single operation.
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2)
Above code is a example for setting ImplicitWait.
Each driver class receives options by constructor or Manage method.

Explicit Wait
Explicit wait has more waiting options than Implicit wait.
It can change polling interval, specify which exceptions should be handled automatically, customize the timeout message and so on.(selenium)
Code example
// Explicit wait
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/dynamic_controls");
// button
driver.FindElement(By.XPath("/html/body/div[2]/div/div[1]/form[2]/button")).Click();
// fill text
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("/html/body/div[2]/div/div[1]/form[2]/input"))).SendKeys("this is a test");
Console.WriteLine("Success");
Behavior
Above codes are example of Explicit waiting and video is actual behavior(saucelabs).
A textbox becomes enable after clicking Enable button. If I don’t specify Explicit Waiting, I got Open QA.Selenium.ElementNotInteractableException because the textbox doesn’t become Enable.
Implicit wait vs Explicit wait

In generally, Explicit wait is more useful than Implicit wait since they provide some options(e.g., polling intervals, timeout time) can handle more conditions.
But, Implicit Wait can set easier than Explicit wait, so if you need to care a conditions whether element is existed, you should set global setting by Implicit wait.
But, there are mentions that mixing implicit and explicit waits cause unpredictable wait times, so we should use only one of them (Selenium, Waiting Strategies).
Summary
- Perhaps the most common challenge for browser automation is ensuring that the web application is in a state to execute a particular Selenium command as desired.
- Implicit wait is a more easy way to wait for ensuring that a element is existed.
- Explicit wait provides more functionality such as polling interval, ensuring conditions such as element is clickable, exception handling, and so on.
- Implicit wait is appropriate when you want to only wait for a element will exist; in other case, Explicit wait is more appropriate.
コメント