Js Node Selenium Try-Catch Js Error not working

62
July 25, 2022, at 3:50 PM

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);
}
Answer 1

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);
}
Rent Charter Buses Company
READ ALSO
How to convert s3Object into File and send it inside a FormData on nodeJS?

How to convert s3Object into File and send it inside a FormData on nodeJS?

so I need to download a file from S3 bucket and then, with either its buffer or readStream, append it to a FormData on nodeI've tried different libraries such as formdata-node, isomorphic-form-data and form-data

86
How do I stop my nodejs server for test purposes?

How do I stop my nodejs server for test purposes?

my nodejs module starts a server on a specific port, I am wondering if it is a way to stop the execution of that server in my mocha test:

61
Get return from .mjs script to Python

Get return from .mjs script to Python

I need to executemjs script about 200 times with another parameters (changableParametr):

94
render page after for loop is finished node.js

render page after for loop is finished node.js

I'm having an async problem, and I've followed a lot of articles online to try and get this issue fixed, but it hasn't done anything

49