Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search then Click on Element
#1
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_SS1C...Gs2LfMmzUM

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

IS this possible?
Reply
#2
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
Reply
#3
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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)