等待 jQuery DataTable 加载 Selenium

Wait for jQuery DataTable loaded with Selenium

我想知道是否有 XPath 或 Css 选择器的方法,是否有一致的方法等待 jQuery DataTable 完成加载与 Selenium.

一起使用

DataTables 提供了一个 initComplete function:

...to know when your table has fully been initialised, data loaded and drawn

您可以将它与 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() returns 'loaded' 和 php 匿名函数 returns true and Selenium 停止等待。如果在 $timeoutInSeconds 秒后未加载 DataTable,将抛出异常“$timeoutInSeconds 后仍未加载 DataTable”。留言。

希望对您有所帮助。

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

This event is fired when DataTables is doing some kind of processing - be it, sorting, filtering or any other kind of data processing

  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);

在 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!");
        }
    }