【问题标题】:How to capture xmlhttprequests pending count in javascript如何在 javascript 中捕获 xmlhttprequests 挂起计数
【发布时间】:2021-01-15 08:45:39
【问题描述】:

如前所述,如何在 javascript 中捕获 xmlhttprequests 未决计数。我在 python 中使用 selenium。

想要捕获等待完成的 xmlhttprequests 的数量。

找到一个有用的链接并尝试了答案中提到的以下javascript。但是没有正确理解并且总是将 XMLHttpRequest.active 设为零。谁能解释一下逻辑或任何其他建议。

Get count of network calls happening in a web page

来自用户https://stackoverflow.com/users/2887618/florent-b

我已经包含了动态注入屏幕的javascript。

jscript = ("(function() {" + 
"  var send = XMLHttpRequest.prototype.send;" + 
"  var release = function(){ --XMLHttpRequest.active };" + 
"  var onloadend = function(){ setTimeout(release, 1) };" + 
"  XMLHttpRequest.active = 0;" +
"  XMLHttpRequest.prototype.send = function() {" + 
"    ++XMLHttpRequest.active;" + 
"    this.addEventListener('loadend', onloadend, true);" + 
"    send.apply(this, arguments);" +
"  };})();")

print(jscript)

driver.execute_script(jscript)

isxmlhttpactive  = driver.execute_script("return (window.XMLHttpRequest.active);")

【问题讨论】:

    标签: javascript python-3.x selenium-webdriver xmlhttprequest


    【解决方案1】:

    目前我已使用以下逻辑来查找任何活动的 xhr 请求处于待处理状态

                from bs4 import BeautifulSoup
                from requests import get
                from selenium import webdriver
                from msedge.selenium_tools import Edge, EdgeOptions
                import difflib
                import time
    
    
                path = r'c:/msedgedriver.exe'
                options = EdgeOptions()
                options.use_chromium = True
                options.add_argument('disable-gpu')
                driver = Edge(executable_path=path, options=options)
                driver.get(url)
    
                is_jquery_present = driver.execute_script("return (typeof jQuery != 'undefined');")
    
                if is_jquery_present == True:
                    is_active_ajax_request  =   driver.execute_script("return ($.active);")
                    
    
                while is_active_ajax_request >= 1:
                    time.sleep(2)
                    is_active_ajax_request =   driver.execute_script("return ($.active);")    
    

    【讨论】:

      猜你喜欢
      • 2019-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      • 2013-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多