Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

c# - How to combine implicit and explicit timeouts in Selenium?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...