运行一个星期前升至更久前的脚本,,,出现如下错误提示
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); }