Ways to retrieve the pending count of XMLHttpRequests in JavaScript

I am trying to figure out how to capture the count of pending xmlhttprequests in a web page using javascript while working with selenium and python.

It seems like there is a useful link that could help me get this information, but I'm having trouble getting the logic right. The XMLHttpRequest.active always returns zero for me. Can someone explain what I might be missing or suggest a different approach?

Get count of network calls happening in a web page

Shared by user

To dynamically inject the required JavaScript code into the screen, I have included the following script:

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

Answer №1

In my current approach, I have implemented the following logic to identify any ongoing active XHR requests:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing my node.js to fail in producing a true result within the return statement?

I've encountered an issue with VS code where the return true command is not displaying anything in my terminal, while console.log(map[arr2[j]]) successfully returns true. I'm unsure if this problem lies with node or my terminal. How can I ensure ...

Guide to generating a PDF file and utilizing a Node.js program for the download process

Seeking assistance in creating a button using my Node.js application to generate a downloadable PDF file filled with information from a PostgreSQL database. Any help is greatly appreciated! ...

Prevent redirection on buttons within ionic lists

THE ISSUE: I am facing an issue in my Ionic app where I am using the ion-list component. Each item in the list can be swiped to reveal a button (add/remove to favorites). Additionally, each item serves as a link to another page. The problem arises when ...

What is the reason behind the NextJS logo not being displayed when accessing the page via its URL?

My logo isn't displaying on the /page URL View Screenshot Check out my components/LayoutWrapper.js import Image from 'next/image' import icon from '../assets/images/Icon.svg' <div className="flex items-ce ...

The NestJS error code 413 indicates that the payload size is too large for

I am currently utilizing nestjs and have encountered an issue where an endpoint only accepts files via multipart. When attempting to upload files that are less than 10MB, everything works perfectly fine. However, when trying to upload files larger than 10M ...

Executing a MongoDB query can only be successful by utilizing the copy and paste method

Recently, I encountered an unusual issue with MongoDB while attempting to query the database. Specifically, when running a query like db.departments.find({"key": "MEL 301"}), it returned no results. The MongoDB database is hosted on mLab, allowing me to v ...

Learn how to seamlessly integrate React Hooks Form with the Select component from @Mui for a powerful

I am currently facing an issue while using react-hooks-form to manage forms in conjunction with the @Mui library. The specific problem I'm encountering is figuring out how to integrate the Select component from the @Mui library with react-hooks-form s ...

What issues can arise in JavaScript if the URL length is not zero when there is no match found?

Upon clicking the "Open Windows" button in Code A, I expected the two links to open in two tabs simultaneously in Chrome, which is exactly what happened. However, when I added a blank line in the textarea control in Code B, only the link http:// ...

It seems that Firefox is ignoring the word-wrap style when the class of a child element is changed

Take a look at this: var iconIndex = 0; var icons = ['check', 'chain-broken', 'flag-o', 'ban', 'bell-o']; $('button:eq(0)').click(function() { iconIndex = (iconIndex + 1) % icons ...

The MUI next Tooltip fails to display upon hovering

While using Material-UIv1.0.0-beta.34 Tooltip with Checkbox and FormControlLabel, I noticed that the tooltip works as expected when hovering over the label in one case. However, when I tried creating a new component(custom) with FormControlLabel and Checkb ...

What is the best method for automating the transfer of data from a database to the user interface of a website using html and javascript?

As a volunteer coordinator for a non-profit organization, I have some basic experience working with Python. I am willing to dedicate fewer than 8 hours of learning time to my current project in order to automate certain tasks (or else I will do them manual ...

Error: Angular does not recognize x2js XML format

I'm attempting to adapt this particular example to utilize xml as json data. However, I am encountering some issues with the code. courses = x2js.xml_str2json(data); console.log(courses.books.course); $scope.todos = courses.books.course; In the XML ...

Removing items in vue.js

I'm currently in the process of learning Vue.js. This is my first attempt at creating a small to-do application, and I am encountering issues with deleting each individual task upon clicking. Despite watching multiple YouTube tutorials, I have not bee ...

What is the best way to retrieve a MariaDB query result using Node.js?

I've been delving into the world of Node.js to enhance my web development skills, particularly in retrieving data from a mariaDB using SELECT queries and converting it into JSON for client requests. Despite scouring the internet and stackoverflow, I h ...

Automatically submitting a form in React.js based on certain conditions being met

Does anyone have experience with React login and register form buttons that trigger Redux actions? I'm facing an issue where both actions are being dispatched at the same time when certain conditions are met. Here is my code snippet: const LoginPage ...

Integrate a Facebook Like-box within a customized jQuery modal window

I've been working on inserting the Facebook like-box code into my page and trying to display it within a jQuery modal dialog. Here's the code I'm using: <script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>< ...

retrieve the coordinates of the northwest and southeast corners of a group of markers displayed on a Google Map

Is there a more efficient way to get the NE and SW corners of a set of markers on a Google map without iterating over each marker individually using JavaScript or Google functions? function fnSetBounds(){ var lowLat = 90; var highLat ...

How to capture text outside of a HTML tag using Selenium with Python?

I need help extracting the specific text inside a div element that is not enclosed by a label. <div style="color:red;"> <label> Total Amount Due:</label> $0.00 </div> Only interested in retrieving the $0.00 amount from th ...

What could be causing the visibility issue with my navigation items in the bootstrap navigation bar?

Currently, I am experiencing a puzzling issue with the navigation bar on my live website. The navigation items seem to flicker for a brief moment and then disappear entirely. This unexpected behavior is certainly causing me some concern. I crafted the us ...

CORS headers not functioning as expected for Access-Control-Allow-Origin

Can someone help me figure out how to add Access-Control-Allow-Origin: 'http://localhost:8080' in Node.js and Express.js? I keep getting this CORS error: Access to XMLHttpRequest at http://localhost:3000 from origin 'http://localhost:8080&ap ...