zoukankan      html  css  js  c++  java
  • Selenium 脚本稳定性问题

    运行一个星期前升至更久前的脚本,,,出现如下错误提示

    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);
            }
  • 相关阅读:
    继承与派生
    函数,迭代器,生成器
    面向对象
    函数
    模块与包
    python面试题(转自https://www.cnblogs.com/wupeiqi/p/9078770.html)
    python学习大纲目录(转自alex博客https://www.cnblogs.com/alex3714/)
    Mysql(基础篇)
    初识HTML和CSS2
    初识前端作业1
  • 原文地址:https://www.cnblogs.com/cloud-test/p/3449749.html
Copyright © 2011-2022 走看看