相当于javascript Selenium Web Driver中的jQuery.active

शेख*_*ेखर 15 javascript jquery selenium selenium-firefoxdriver selenium-webdriver

我正在使用Selenium webdriver与某些网站进行交互.
如果网站使用jQuery,我们可以通过使用获取待处理的AJAX请求jQuery.active.

 JavascriptExecutor jsx = (JavascriptExecutor) driver;
Run Code Online (Sandbox Code Playgroud)

Int totAjaxRequest =(Int)jsx.executeScript("jQuery.active");

Int totAjaxRequest = (Int)jsx.executeScript("return jQuery.active");
Run Code Online (Sandbox Code Playgroud)

如果网站没有使用jQuery,我们如何计算XMLHttpRequest请求的数量.

Voi*_*rit 1

将其保存在您的网站中,并从 selenium 中调用它。我认为js中没有类似的内置函数。我认为这不是您正在寻找的答案,但正如我上面所说,javascript 本身没有这样的功能。

如果您无法编辑脚本或向网站添加新脚本,您仍然可以运行该脚本。

Int totAjaxRequest = (Int)jsx.executeScript("
    (function(){
        var count = 0;
        XMLHttpRequest.prototype.nativeSend = XMLHttpRequest.prototype.send;
        XMLHttpRequest.prototype.send = function(body) {

            this.onreadystatechange  = function(){
               switch(this.readyState){
                   case 2: count++; break
                   case 4: count--; break
               }
            };
            this.nativeSend(body);
        };

        return count;
    })()

");
Run Code Online (Sandbox Code Playgroud)