Python
PythonExecutor py = (PythonExecutor) driver;
Boolean ready = (Boolean)py.executeScript("the following Python code");
Python Code
var ready = False;
window.onload = def check_ready():
ready = True
def sleep():
return new Promise(resolve => setTimeout(resolve, 2000))
for i in range(30):
if ready:
return True
await sleep()
return False;
UPDATE: Apologies for the syntax error "funtion" in my previous post. It was a typo and not part of my actual code. Despite fixing all syntax errors, I still encounter "SyntaxError: Unexpected identifier".
The purpose of this code is to wait for a specified period for the page to finish loading. While I normally use document.readyState to determine this, there are instances where Chrome stops loading abruptly and document.readyState remains stuck for over 5 minutes. To counter this issue, I am experimenting with creating single-threaded code that simulates a multi-threaded process.
Given JavaScript's single-threaded nature (a limitation for such a versatile language), we must think outside the box.
This code functions properly in the browser console when replacing return true;
with console.log('true');
and return false;
with console.log('false');
, so the problem doesn't seem apparent.