2

I am attempting to write a script to automate some tasks on a third party site. The very first step is simply clicking a div on the nav, but document.getElementById('myId').click() does nothing.

In my searching I found this answer that fully simulates a mouse click: Simulating a mousedown, click, mouseup sequence in Tampermonkey?

However, that also does not work. I did notice that there's a class added when hovering, and the script successfully simulates that. And obviously physically clicking works fine. I'm not sure what else I could be missing

Edit: It turns out that the clicking was just fine, but the site is actually checking pieces of the event such as the coordinates, which is why it appeared to not be functioning properly

ELepolt
  • 373
  • 2
  • 4
  • 16
  • check if the element has not any click preventing css properties like pointer-events to none or not. – Shashank Bhatt Jun 08 '20 at 15:31
  • You might be running your script before the element with id="myId" has loaded. Make sure your script runs AFTER the body has finished loading. – Olian04 Jun 08 '20 at 16:12
  • Definitely not the case, as I'm attempting to test this by using the console, so the element is definitely loaded. – ELepolt Jun 08 '20 at 16:15
  • @ShashankBhatt It doesn't appear to have anything blocking it. And I can manually click the div and it works fine. It's only when attempt to do it via JS. – ELepolt Jun 08 '20 at 16:17

1 Answers1

1

If I understand correctly, what you want is this:

document.getElementById('myId').addEventListener('click', myFunction);

This will run the function myFunction() when the element with the ID of "#myId" is clicked.

Hope this helps!

P.S. If this doesn't work, I suggest using Shashank Bhatt's suggestion of checking pointer-events.

Tilier
  • 80
  • 7
  • It's not so much that I want a function to be run when it's clicked, it's that I want the functionality of clicking the div. Its use is to navigate to another page. So clicking it manually works, but trying to automate it does not. – ELepolt Jun 08 '20 at 16:14
  • Okay! Please excuse me if I still don't understand you, but do you mean that you want to link the div to a URL? – Tilier Jun 08 '20 at 16:20
  • I'm attempting to click a link via javascript. IE: If I inspect the 'tags' link on the side bar here, I can do `$0.click()` and it will take me to that page. That is what I'm attempting to do on this third party site, but `click()` is not doing anything. https://i.imgur.com/XqMh3yW.png – ELepolt Jun 08 '20 at 16:28