如何在Selenium中组合隐式和显式超时? [英] How to combine implicit and explicit timeouts in Selenium?

查看:130
本文介绍了如何在Selenium中组合隐式和显式超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有隐式超时的Selenium ChromeDriver:

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

我希望这需要120秒才能尝试找到该元素,但是5秒钟后就会超时.

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

推荐答案

根据

请勿混合 隐式 显式 等待.这样做可能导致不可预测的等待时间.例如,将隐式等待设置为10秒,将显式等待设置为15秒,则可能导致20秒后发生超时.

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.

如果您将隐式超时定义为:

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

这篇关于如何在Selenium中组合隐式和显式超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆