C#Selenium’ExpectedConditions已过时’

当尝试使用ExpectedConditions显式等待元素变得可见时,Visual Studio警告我它现在已经过时,很快就会从Selenium中删除。

实现相同结果的当前/新方法是什么?

var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30)); var element = wait.Until(ExpectedConditions.ElementIsVisible(By.Id("content-section"))); 

我解决了自己的问题,想为其他人提供答案,想知道如何使用最新版本的Selenium来解决这个问题。

使用nuget,搜索DotNetSeleniumExtras.WaitHelpers,将该命名空间导入您的类。 现在你可以这样做:

 var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30)); var element = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.Id("content-section"))); 

IDE中的警告将消失。

如果您不想下载额外的nuget包,则很容易声明自己的函数(或条件),尤其是使用lamda表达式,例如

 var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30)); var element = wait.Until(condition => { try { var elementToBeDisplayed = driver.FindElement(By.Id("content-section")); return elementToBeDisplayed.Displayed; } catch (StaleElementReferenceException) { return false; } catch (NoSuchElementException) { return false; } }); 

这也是非常多才多艺,因为现在可以评估任何类型的bool表达式。

这很简单,只是改变

 Wait.Until(ExpectedConditions.ElementIsVisible(By.Id("content-section"))); 

 Wait.Until(c => c.FindElement(By.Id("content-section"))); 

检查您安装了哪个版本的Selenium.Support和Selenium.WebDriver NuGet Package。 我现在用最新版本3.11.2得到了同样的问题,我降级到3.10.0并修复了问题。