Friday, 17 March 2017

Node Js Phantom click button

I'm running Phantom as a child process in Node. For this I'm using the following plugin. The main thing I'm trying to achieve is to simulate a button click for those elements who matches a specific condition. For the first example I'm trying to pass in a simple class name and then click the elements that has that very class.

Here is how I have set up the childprocess:

router.post("/nodeurlasync", function(req, res){
    var url = req.body.url;
    var phantomchild = exec('node ./nodephantomhandlers/headertesturlclick.js ' + url + ' ' + '.btn');

    phantomchild.stdout.pipe(process.stdout);
    phantomchild.on('exit', function(data) {
        console.log(data);        // process.exit();
    });
    phantomchild.stdout.on('data', function(data) {
        console.log('stdout success: ' + data);
        res.send(data);
    });
    phantomchild.stderr.on('data', function(data) {
        console.error('stdout err: ' + data);
    });
    phantomchild.on('close', function(code) {
        console.log('closing code: ' + code);
        res.end("exited with code: " + code);
    });
});

Here's the headertesturlclick.js file:

var phantom = require('phantom'), 
    fs = require("fs"),
    url
      scriptPath = __dirname + "/files/scripts/";
if (process.argv.length === 1) {
  console.log('Usage: [0] <some URL>');
  return;
}
url = process.argv[2];

var eventTarget = process.argv[3];
    phantom.create().then(function(ph) {
   ph.createPage().then(function(page) {
      page.open(url).then(function(status) {
      page.evaluate(function(eventTarget) {
        console.log(eventTarget);
          var target = window.document.getElementsByClassName(eventTarget);
              window.document.getElementsByClassName('btn').click();
                 var element = document.getElementsByClassName('mybutton');
                            var ev = document.createEvent("MouseEvent");
                            ev.initMouseEvent(
                                "click",
                                true /* bubble */, true /* cancelable */,
                                window, null,
                                0, 0, 0, 0, /* coordinates */
                                false, false, false, false, /* modifier keys */
                                0 /*left*/, null
                            );
                            element.dispatchEvent(ev);
                             setTimeout(function() {
                              return window.document.body.innerHTML;
                            }, 3000);
          // return window.document.body.innerHTML;
      }).then(function(html){
        console.log(html);
      });
    }).then(function(){
      page.close();
      ph.exit();
    });

  });
});

My first question: How do i click an element inside the evaluate function?

My main question: How do I pass the eventTarget variable into the evaluate function and then simulate a click event for the matching elements? When I try to access it inside the evaluate function its null.

heres the output I'm getting:

Phantom server up and running on 8080 info: TypeError: undefined is not a constructor (evaluating 'window.document.getElementsByClassName('btn').click()') info:
undefined:4 in anonymous info: :21 stdout success: info: TypeError: undefined is not a constructor (evaluating 'window.document.getElementsByClassName('btn').click()') info:
undefined:4 in anonymous info: :21

null stdout success: null

0 closing code: 0

Thanks in advance.



via Carl91

No comments:

Post a Comment