Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium c#:等待元素出现而不等待给定的时间,否则超时_C#_Selenium_Selenium Webdriver_Element_Wait - Fatal编程技术网

Selenium c#:等待元素出现而不等待给定的时间,否则超时

Selenium c#:等待元素出现而不等待给定的时间,否则超时,c#,selenium,selenium-webdriver,element,wait,C#,Selenium,Selenium Webdriver,Element,Wait,为了进一步解释,我目前正在研究硒和碳。我的问题是,我的工具运行得非常快,不需要等待元素准备就绪 例如,不建议使用Thread.Sleep() 给定的等待时间为2秒。元素将在1秒或更长时间内出现。在线程.Sleep()之后,因此代码行不可靠 或者元素存在,但仍在等待完成Thread.Sleep(),因此非常耗时 我想要的是,如果元素被找到,那么就不需要在给定的时间等待,如果在给定的时间没有找到,那么就超时。这正是and的作用所在 以身作则 WebDriverWait wait = new WebD

为了进一步解释,我目前正在研究硒和碳。我的问题是,我的工具运行得非常快,不需要等待元素准备就绪

例如,不建议使用
Thread.Sleep()

给定的等待时间为2秒。元素将在1秒或更长时间内出现。在
线程.Sleep()之后,因此代码行不可靠

或者元素存在,但仍在等待完成
Thread.Sleep()
,因此非常耗时

我想要的是,如果元素被找到,那么就不需要在给定的时间等待,如果在给定的时间没有找到,那么就超时。

这正是and的作用所在

以身作则

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By locator));
这将等待5秒,元素才可见。如果成功,元素将返回,否则将抛出
TimeoutException

更新


ExpectedConditions
已移动,现在位于SeleniumExtras.WaitHelpers

“ExpectedConditions”已过时:“ExpectedConditions” .NET绑定中的实现已弃用,将被删除 在未来的版本中。这部分代码已迁移到 GitHub上的DotNetSeleniumExtras存储库 ()'


要避免对现有代码进行许多更改,请将
ExpectedConditions
导入名为
ExpectedConditions
的变量。代码的其余部分保持不变

using ExpectedConditions = SeleniumExtras.WaitHelpers.ExpectedConditions;

ExpectedConditions
现已弃用。你有其他的解决办法吗?