【问题标题】:Wait for jQuery DataTable loaded with Selenium等待加载了 Selenium 的 jQuery DataTable
【发布时间】:2017-05-19 19:31:01
【问题描述】:

我想知道是否有使用 XPath 或 CssSelectors 的方法,是否有一致的方法可以等到 jQuery DataTable 完成加载以便与 Selenium 一起使用强>。

【问题讨论】:

  • 不需要,因为所有 jQuery DataTables 共享相同的骨架

标签: jquery selenium datatables wait


【解决方案1】:

DataTables 提供了一个initComplete function:

...知道您的表何时已完全初始化、数据加载和绘制

您可以将它与 Selenium 的 wait()->until() 方法结合使用。下面是一个使用 php 的例子:

$this->webDriver->wait($timeoutInSeconds, $intervalInMilliseconds)->until(
        // a php anonymous function executed every $intervallInMilliseconds
        // it tells Selenium to execute some JS in the brower
        // when this methods returns true, Selenium stops waiting
        function(){
            return 'loaded' === func_get_arg(0)->executeScript(
                // the javascript method provided by DataTable
                "$('#dataTableId').dataTable( {
                    \"initComplete\": function( settings, json ) {
                        return 'loaded';
                    }
                }
            );");
        },
        "DataTable still not loaded after $timeoutInSeconds."
 );

加载DataTable时,js $('#dataTableId').dataTable() 返回'loaded',php匿名函数返回true,Selenium停止等待。如果在 $timeoutInSeconds 秒后未加载 DataTable,则会抛出“在 $timeoutInSeconds 后仍未加载 DataTable”的异常。消息。

希望这会有所帮助。

【讨论】:

  • 谢谢。我将检查我是否可以在 C# 中以某种方式使其工作
【解决方案2】:

https://datatables.net/reference/event/processing

当 DataTables 进行某种处理时会触发此事件 - 无论是排序、过滤还是任何其他类型的数据处理

  public static void WaitUntilDataTableHasFinishedLoading(this IWebDriver driver, IWebElement processingTable)
    {
        processingTable.WaitForAttribute("style", "display: none;");
    }

用法:

       IWebElement x = SeleniumInfo.Driver.FindElement(By.Id("tableName_processing"));
SeleniumInfo.Driver.WaitUntilDataTableHasFinishedLoading(x);

【讨论】:

    【解决方案3】:

    在java中你可以这样做

        //Wait for JQuery Load
        public static void waitForJQueryLoad() {
            //Wait for jQuery to load
            ExpectedCondition<Boolean> jQueryLoad = driver -> ((Long) ((JavascriptExecutor) jsWaitDriver)
                    .executeScript("return jQuery.active") == 0);
    
            //Get JQuery is Ready
            boolean jqueryReady = (Boolean) jsExec.executeScript("return jQuery.active==0");
    
            //Wait JQuery until it is Ready!
            if(!jqueryReady) {
                System.out.println("JQuery is NOT Ready!");
                //Wait for jQuery to load
                jsWait.until(jQueryLoad);
            } else {
                System.out.println("JQuery is Ready!");
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多