Antwise community forums
Search then Click on Element - Printable Version

+- Antwise community forums (https://forums.antwise.com)
+-- Forum: Antview (https://forums.antwise.com/forumdisplay.php?fid=11)
+--- Forum: Antview for Windows (https://forums.antwise.com/forumdisplay.php?fid=12)
+--- Thread: Search then Click on Element (/showthread.php?tid=181)



Search then Click on Element - maddire - 2024-01-07

Hi, is there a method to Raise a click event of a Button based on the class and innerHTML of an element?

Example: I can navigate to the following URL: https://mega.nz/#!20pzDaAQ!dNuODm_0_SS1CAGUULeEprlMXPSSsYGYbGs2LfMmzUM

But I want to raise the Click event on the Download Button. 

IS this possible?


RE: Search then Click on Element - wila - 2024-01-08

Hi,

Not sure yet how-to do this.

I can get the button object in javascript like so:
Code:
var downloadButton;
downloadButton = document.getElementsByClassName("mega-button positive js-default-download js-standard-download");

but that doesn't mean I can invoke a click on it as the button object does not have the method.

Next up I tried:
Code:
function simulateClick() {
  const event = new MouseEvent("click", {
    view: window,
    bubbles: true,
    cancelable: true,
  });
  const cb = document.getElementsByClassName("mega-button positive js-default-download js-standard-download");
  const cancelled = !cb.dispatchEvent(event);

  if (cancelled) {
    // A handler called preventDefault.
    alert("cancelled");
  } else {
    // None of the handlers called preventDefault.
    alert("not cancelled");
  }
}
which then gave me "Uncaught TypeError: cb.dispatchEvent is not a function"

So for the moment I don't know.
--
Wil


RE: Search then Click on Element - wila - 2024-01-09

Maddire,

Figured to have another look at this and saw my mistake...

The code:
Code:
var downloadButton;
downloadButton = document.getElementsByClassName("mega-button positive js-default-download js-standard-download");
does not just return a html element, instead it returns a HTMLCollection array... an array with 1 element.
So my mistake was to try and send "click" from the array instead of the html element.

Just tested it and this code works fine:
Code:
var downloadButton;
downloadButton = document.getElementsByClassName("mega-button positive js-default-download js-standard-download");
downloadButton[0].click();

Obviously it could use some error checking, but the principle works fine.
It triggers a download just like you are after.

--
Wil