1

I'm pretty new to PhantomJS, so I'm just trying to stumble through some practical examples (the documentation feels a little light on those).

One thing I was attempting was to submit votes to a phony PollDaddy poll, but I'm struggling. Using the script below, I can tell from the first screenshot that my option button is being clicked/selected. But why does my answer not get submitted? The second screenshot looks identical to the first, despite my code executing a click event on the VOTE button in the second evaluate. Anyone know why? Know how to make it work?

var webPage = require('webpage');
var page = webPage.create();

//******* BEGIN LOGGING METHODS *******    
// http://phantomjs.org/api/webpage/handler/on-url-changed.html
page.onUrlChanged = function(targetUrl) {
  console.log('New URL: ' + targetUrl);
};

// http://phantomjs.org/api/webpage/handler/on-console-message.html
page.onConsoleMessage = function(msg, lineNum, sourceId) {
  console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};

// http://phantomjs.org/api/webpage/handler/on-error.html
page.onError = function(msg, trace) {
  var msgStack = ['ERROR: ' + msg];
  if (trace && trace.length) {
    msgStack.push('TRACE:');
    trace.forEach(function(t) {
      msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
    });
  }
  console.error(msgStack.join('\n'));
};

// http://phantomjs.org/api/webpage/handler/on-resource-error.html
page.onResourceError = function(resourceError) {
  console.log('Unable to load resource (#' + resourceError.id + ' URL:' + resourceError.url + ')');
  console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};

// http://phantomjs.org/api/webpage/handler/on-resource-timeout.html
page.onResourceTimeout = function(request) {
    console.log('Response Timeout (#' + request.id + '): ' + JSON.stringify(request));
};
//******* END LOGGING METHODS *******    

page.open('https://polldaddy.com/poll/9424638/', function(status) {
    console.log('Status: ' + status);

    //make selection    
    var myselection = page.evaluate(function() {
        var ev = document.createEvent("MouseEvent");
        ev.initMouseEvent("click", true, true, window, null,0, 0, 0, 0, false, false, false, false, 0, null);

        //radio button for Curly has id=PDI_answer42988707
        var myselection = document.querySelector("#PDI_answer42988707");
        (myselection).dispatchEvent(ev);
        return document.title;
    });
    //screen capture the selection
    page.render('selection.png');
    console.log(myselection);

    //click the Vote button
    var dovote = page.evaluate(function() {
        var ev = document.createEvent("MouseEvent");
        ev.initMouseEvent("click", true, true, window, null,0, 0, 0, 0, false, false, false, false, 0, null);

        //get a handle to the vote button
        var votebutton = document.querySelector("a.vote-button");
        //click the vote button
        (votebutton).dispatchEvent(ev);
        return document.title;
    });
    //delay, then take screenshot...
    setTimeout(
        function(){
            page.render('voted.png');
            console.log(dovote);
        }
        ,2000
    );

});

I'm executing the script using PhantomJS 1.9.0 on Linux Mint. The execution parameters are like this to overcome any SSL Handshake failures:

phantomjs --ignore-ssl-errors=true --ssl-protocol=any myscript.js

I've also tried setting --cookies-file and --local-storage-path on the command line, but that didn't help either.

The output I get from all of the console logging above is this:

New URL: https://polldaddy.com/poll/9424638/  <-- from urlChange
CONSOLE: JQMIGRATE: Logging is active (from line # in "") <-- from onConsoleMessage
Status: success
Who is your favorite Stooge (poll 9424638) | Polldaddy.com
Who is your favorite Stooge (poll 9424638) | Polldaddy.com

My useless poll is here: https://polldaddy.com/poll/9424638/, and you'll see from my script that I'm trying to stack the results for Curly.

Marc
  • 11,403
  • 2
  • 35
  • 45
  • Any reason you're using v1.9.0? It's ancient. You should try 2.1.1, 1.9.8, 1.9.7 in that order. – Artjom B. May 22 '16 at 10:40
  • Thanks, @ArtjomB. I was using the one from the Synaptic package manager in Mint. However, I just manually upgraded to 2.1.1 (the latest on the download site), but the issue remains. Any other ideas? – Marc May 22 '16 at 17:51
  • Please register to the `onConsoleMessage`, `onError`, `onResourceTimeout` events ([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf#file-1_phantomerrors-js)). Maybe there are errors. – Artjom B. May 22 '16 at 19:26
  • @ArtjomB.: Done. And added to the question. I'm not sure it helps though :( – Marc May 22 '16 at 19:58
  • You may try other types of clicks, particularly: http://stackoverflow.com/a/24026636/1816580. If it has a form around it (I haven't looked at the page), you may also try to submit that form. – Artjom B. May 22 '16 at 20:03

1 Answers1

0

If you look at the javascript that is being included with the polldaddy page, you'll find a vote function with the following check:

if (event.pageX) {
  eventX = event.pageX;
  eventY = event.pageY;
} else {
  eventX = event.clientX;
  eventY = event.clientY;
}

...

if (eventX == 0 && eventY == 0) {
  return false
}

Basically they are looking at the event location and short-circuiting if the x and y location are both equal to 0.

If you change the mouse event that you create so that the clientX or clientY value is not 0, then your script should work.