Simulate human click in JavaScript

I have a small scraper where I need to click the anchor link using JavaScript. I tried several ways: jQuery.click() , document.createEvent('MouseEvents') , etc. They all worked, but they are not fully executed as a human click (they open the tab as they should, but they do not start the download).

An anchor tag has this attribute:

 onclick="if (document.getElementById('ReportViewer_ctl01_ctl05_ctl00').selectedIndex == 0) return false; if (!ClientToolbarReportViewer_ctl01.HandleClientSideExport()) __doPostBack('ReportViewer$ctl01$ctl05$ctl01','');return false;" 

I also tried running it on the command line:

  __doPostBack('ReportViewer$ctl01$ctl05$ctl01','') 

it is also a kind of work, but not completely like a flick of a person.

I can tell you in detail if necessary, but at the moment I'm looking for a magic bullet, which, I think, should exist.

+4
source share
1 answer

I keep pastine saved from two programmatic ways to do this. It only once bothered me when google decided to remove the window object (and every other object) of its default functions>. >

http://pastebin.com/VMHvjRaR

 function callClickEvent(element){ var evt = document.createEvent("HTMLEvents"); evt.initEvent("click", true, true); element.dispatchEvent(evt); } function callClickEvent2(element){ var evt = document.createEvent("MouseEvents"); evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); element.dispatchEvent(evt); } callClickEvent(document.getElementById("myElement")) callClickEvent2(document.getElementById("myElement")) 

MDN Documentation:

+4
source

All Articles