?
運行一個星期前升至更久前的腳本,,,出現如下錯誤提示
elementNotVisibleException was unhandled by user code。
經過詢問大俠,說我的腳本不夠健全,也就是穩定性的問題,需要增加一些判斷等待,那需要封裝一些等待方法
?
public static void WaitUnit<T>( this IWebDriver webDriver, Func<IWebDriver, T> func, int seconds = 20 ) { var wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(seconds)); wait.Until(func); } /// <summary> /// 強制讓webdriver等待,直到給定的元素(by)存在于DOM之中 /// </summary> public static void WaitElementExist( this IWebDriver webDriver, By by, int seconds = 20 ) { var wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(seconds)); wait.Until(ExpectedConditions.ElementExists(by)); } /// <summary> /// 強制讓webdriver等待,直到給定的元素(by)的CSS屬性visiblity的值為visible /// </summary> public static void WaitElementVisible( this IWebDriver webDriver, By by, int seconds = 20 ) { var wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(seconds)); wait.Until(ExpectedConditions.ElementIsVisible(by)); } /// <summary> /// 強制讓webdriver等待,直到當前的瀏覽器title包含指定的字符串 /// </summary> public static void WaitTitleContains( this IWebDriver webDriver, string title, int seconds = 20 ) { var wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(seconds)); wait.Until(ExpectedConditions.TitleContains(title)); } /// <summary> /// 強制讓webdriver等待,直到當前的瀏覽器title與給定讀的title一致 /// </summary> public static void WaitTitleIs( this IWebDriver webDriver, string title, int seconds = 20 ) { var wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(seconds)); wait.Until(ExpectedConditions.TitleIs(title)); } /// <summary> /// Executes JavaScript in the context of the currently selected frame or window. /// </summary> /// <param name="webDriver"> IWebDriver實例 </param> /// <param name="jsCode"> js代碼(最好的web里面先執行一次,確保正確性) </param> public static void ExecJavascript( this IWebDriver webDriver, string jsCode) { ((IJavaScriptExecutor)webDriver).ExecuteScript(jsCode); }
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
