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.