Implementing "Network Idle" Wait without WebDriver BiDi #194
Replies: 2 comments
-
|
I overlooked including DOM mutations in the monitoring scope, so I have revised the code as follows. Note: As a control experiment,, commenting out the following two lines will prevent the driver from waiting until the process completes. note:Analyzing the JavaScript in this example reveals a 500ms delay after 'Done!' is displayed. Therefore, I have set the idle timeout to 600ms—slightly longer than the 500ms threshold—to ensure proper synchronization.Refer to the next comment for instructions on how to configure this. Example Page: |
Beta Was this translation helpful? Give feedback.
-
|
Even when using classic commands, BiDi can be leveraged as a wait strategy tool. Running the following code generates logs in the Immediate Window; by pasting these into an AI and asking 'What is the most efficient idle wait time?', you can obtain the optimal setting.
This is the .xlsm file containing the code mentioned above. The password is 123. |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This code utilizes a technique known as "Network Idle" to accurately wait for asynchronous browser processes—specifically communications via the Fetch API—to fully complete.
Instead of relying on a fixed pause (Sleep), it monitors the start and end of network requests on the JavaScript side. Control is only returned to VBA once a specified period of silence (e.g., 500ms with no active requests) has been detected.I think this is a very useful method when you aren't using bidirectional communication via WebDriver BiDi.
Key Points of the Wait Logic
Request Monitoring via Hooks (InstallNetworkAndDomHooks)
By overriding window.fetch and XMLHttpRequest, the script creates a global object (window.__niw) that maintains a counter of currently active requests. This makes the internal state of the browser transparent and trackable from the VBA environment.
Efficient Waiting via ExecuteScriptAsync
The use of driver.ExecuteScriptAsync within WaitForIdleStateAsync is a critical design choice:
Asynchronous Execution: A timer runs within the browser context, blocking the VBA execution until the specific condition (s.req === 0) is met.
Timeout Control: By implementing maxTimeToWait (30 seconds), the script ensures that VBA won't hang in an infinite loop if a network request fails to close or the connection remains stalled.
Handling the 5-Second Delay
While the HTML script calls https://httpbin.org/delay/5 (a service that intentionally delays responses for 5 seconds), the execution flows as follows:
Trigger: SelectByValue initiates the network request.
Wait Initiation: WaitForIdleStateAsync is called, putting the VBA thread into a waiting state.
Monitoring: As long as the browser is in a "Requesting" state (req=1), the callback cb('ok') is not triggered.
Completion Judgment: After 5 seconds, the Fetch request completes and req drops to 0. Once the idleTimeout (0.5s) passes without new activity, the "Stable" state is confirmed, and control returns to VBA.
Beta Was this translation helpful? Give feedback.
All reactions