How to combine implicit and explicit timeouts in Selenium?
Asked Answered
M

1

2

I am using Selenium ChromeDriver with an implicit timeout:

_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);

In one of my tests I want to override this with an explicit timeout. Before reading a property I explicitely wait for the element to be found:

WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(120));
wait.Until(d => d.FindElement(By.CssSelector("div.example")));

I would expect this to take 120s to try to find the element, but it times out after just 5 seconds.

Mauri answered 8/10, 2018 at 16:40 Comment(2)
From documentation: "WARNING: Do not mix implicit and explicit waits"Festivity
I agree totally with @Festivity answer, but I think if you absolutely have to, I would first change the implicit wait to also be 120 seconds, then change it back to 5 after what I would assume would be a successful match.Objectify
A
2

As per the documentation of Explicit and Implicit Waits it is clearly mentioned that:

Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10 seconds and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.

Still if you have implicit timeout defined as:

_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);

Before inducing explicit wait for the element to be found you need to remove the implicit timeout as follows:

_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(0);
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(120));
wait.Until(d => d.FindElement(By.CssSelector("div.example")));

Once you are done with the explicit wait, you can re-configure back the implicit timeout again as:

_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(0);
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(120));
wait.Until(d => d.FindElement(By.CssSelector("div.example")));
//perform your action with the element
_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
Apophyllite answered 8/10, 2018 at 18:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.