WebDriverWait不等待我指定的元素 [英] WebDriverWait is not waiting for the element I specify

查看:58
本文介绍了WebDriverWait不等待我指定的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的代码等待一个元素出现,然后再尝试从该元素获取文本.如果我逐步执行允许元素时间出现的代码,则它按预期方式工作,但是如果我在没有断点的情况下运行它,则等待似乎会被忽略,并引发异常.

I am trying to get my code to wait for an element to appear before trying to get the text from the element. If I step through the code allowing the element time to appear it works as expected, but if I run it without breakpoints the wait appears to be ignored and an exception is raised.

我不明白为什么它被忽略了?

I don't understand why it is being ignored?

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
IWebElement message = wait.Until(driver => driver.FindElement(By.ClassName("block-ui-message")));
string messageText = message.Text;

推荐答案

作为替代方案,您可以为 ElementIsVisible()引入 WebDriverWait ,并且可以使用以下定位器策略:

As an alternative you can induce WebDriverWait for the ElementIsVisible() and you can use the following Locator Strategy:

string messageText = new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(ExpectedConditions.ElementIsVisible(By.ClassName("block-ui-message"))).GetAttribute("innerHTML");


使用 DotNetSeleniumExtras.WaitHelpers nuget :

不是很清楚您使用我需要的特定指令的确切含义.如果您使用的是 SeleniumExtras WaitHelpers ,则可以使用以下解决方案:


Using DotNetSeleniumExtras.WaitHelpers with nuget:

Not that super clear what exactly you meant by specific using directive I need. In-case you are using SeleniumExtras and WaitHelpers you can use the following solution:

string messageText = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.ClassName("block-ui-message"))).GetAttribute("innerHTML");

这篇关于WebDriverWait不等待我指定的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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