c# Selenium ExpectedConditions 不存在

Selenium中的顯示等待指的是,等待某一元素出現或者等待頁面加載完成後,才執行下一步。需要用到WebDriverWait類。

例如:

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

但是在.net core最新版本的Selenium下使用WebDriverWait的時候,發現找不到ExpectedConditions。有一個比較簡單的方法就是將Selenium降級到3.10.0或者找一些NuGet包例如DotNetSeleniumExtras.WaitHelpers。如果不想添加新的包或者降級,可以考慮使用Lambda表達式。如下:

複製代碼
 WebDriverWait wait = new WebDriverWait(_firefoxDriver, TimeSpan.FromSeconds(10));//設置超時時間
 var isFInd = wait.Until(contion =>
       {
           try
           {
//尋找元素 _firefoxDriver.FindElement(By.ClassName("c-icon-shape-hover")).Click(); //_firefoxDriver.FindElement(By.XPath(xpath)).Click(); return true; } catch (Exception e) { return false; } }); if (!isFInd) throw new Exception("Cant' Find Click");
複製代碼

當超時了會拋出一個超時異常。

參考鏈接:https://stackoverflow.com/questions/49866334/c-sharp-selenium-expectedconditions-is-obsolete

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章