I'm trying to simulate clicking on an element that is rendered after a certain iteration of document.readyState='complete'. To get around the readyState issue, I thought of try-catching the executeScript command to just make it retry until it works.
But for some reason Node / Selenium just stops working entirely from the resulting Js error:
Cannot read properties of undefined (reading 'parentElement')
Any ideas on how I should solve this? Here's part of my code. It'll open up the supplied url (item) and should execute the command as soon as the element icon-download is rendered.
async function getFileFromUrl(item) {
var command = "document.getElementsByClassName('icon-download')[0].parentElement.click()";
driver.get(item);
do {
await delay(500);
try{
driver.executeScript(command);
} catch (e) {
continue;
}
break;
} while (true);
await delay(3000);
}
Ok so I managed to fix it by prefixing the driver-commands with Await. If they return an exception it's recognized by Node now. Code below:
async function getFileFromUrl(item) {
var command = "document.getElementsByClassName('icon-fpk icon-download-xls')[0].parentElement.click()";
await driver.get(item);
do {
await delay(500);
try{
await driver.executeScript(command);
} catch {
continue;
}
break;
} while (true);
await delay(3500);
}