Alj*_*aro 3 c# selenium element wait selenium-webdriver
为了进一步解释,我目前正在使用 selenium 和 c#。我的问题是,我的工具运行得非常快,无需等待元素准备就绪。
Thread.Sleep()
例如,不建议这样做。
给定的等待时间为 2 秒。该元素将在 1 秒或更长时间内出现。之后的Thread.Sleep()
代码行不可靠。
或者该元素存在但仍在等待完成,Thread.Sleep()
因此非常耗时。
我想要的是,如果找到该元素则无需等待给定时间,如果在给定时间未找到则超时。
使用示例
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By locator));
Run Code Online (Sandbox Code Playgroud)
这将等待最多5 秒以使元素可见。如果成功,该元素将被返回,如果失败,它将抛出TimeoutException
。
更新
ExpectedConditions
已搬迁,现位于SeleniumExtras.WaitHelpers
“ExpectedConditions”已过时:“.NET 绑定中的 ExpectedConditions 实现已弃用,并将在未来版本中删除。这部分代码已迁移到 GitHub 上的 DotNetSeleniumExtras 存储库 ( https://github.com/DotNetSeleniumTools/DotNetSeleniumExtras )'
为了避免对现有代码进行大量更改,请将 导入ExpectedConditions
到名为 的变量中ExpectedConditions
。其余代码保持不变
using ExpectedConditions = SeleniumExtras.WaitHelpers.ExpectedConditions;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9052 次 |
最近记录: |