WebDriverWait is not waiting for the element I specify(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 不等待我指定的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:WebDriverWait 不等待我指定的元素
基础教程推荐
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 将数据集转换为列表 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 如果条件可以为空 2022-01-01
