jsdom async MouseEvent("click") dispatchEvent anchor tags

180 views
Skip to first unread message

Gary

unread,
Feb 14, 2022, 10:37:59 PM2/14/22
to jsdom
All:

What is the best practice for instantiating a MouseEvent("click") on anchor tags using dispatchEvent within an asynchronous fromURL request?

I've successfully configured an asynchronous await fromURL request using a querySelector  to validate when the page is fully loaded (it takes about a minute to load everything).  However, there are several anchor tags that dynamically load pages:

<a href="#" controls="results" dataindex="1" tabindex="0">1</a>
<a href="#" controls="results" dataindex="2" tabindex="0">2</a>
<a href="#" controls="results" dataindex="3" tabindex="0">3</a>

Which I'm trying to use MouseEvent("click") within dispatchEvent to access their results.

dom.window.document.querySelector("a").dispatchEvent(new dom.window.MouseEvent("click"));

However, it seems when I add the dispatchEvent, I receive the following error:

TypeError: Cannot read properties of null (reading 'dispatchEvent')

    at Timeout._onTimeout (/media/user/SanDiskSDXC/pushbot/torproject.org/metrics.js:42:66)

    at listOnTimeout (node:internal/timers:559:17)

    at processTimers (node:internal/timers:502:7)


Node.js v17.4.0

Command exited with non-zero status 1

How does one call an asynchronous page, await for the response, and instantiate a click on the desired anchor tag to access the desired results?

Thank you for your assistance.

Respectfully,


Gary

Gary

unread,
Feb 15, 2022, 4:36:01 PM2/15/22
to jsdom

All:

It seems the dom.window.MouseEvent("click") requires the  { bubbles: true } attribute in this instance.

Changed dispatchEvent from:

dom.window.document.querySelector("a").dispatchEvent(new dom.window.MouseEvent("click"));

Changed dispatchEvent to:

dom.window.document.querySelector("a").dispatchEvent(new dom.window.MouseEvent("click", { bubbles: true }));

Now, I am able to programmatically click-through each anchor tag outputting the desired results to the console (all within the ~1 minute asynchronous request).

Respectfully,


Gary
Reply all
Reply to author
Forward
0 new messages