Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
Js Node Selenium Try-Catch Js Error not working

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);
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!