A website I use PhantomJS to log into and scrape a table out of was recently updated and the new version seems to require key presses to actually occur on the username and password fields to login now.
My old code looks like this:
var page = require('webpage').create();
phantom.cookiesEnabled = true;
page.open("https://website.location.com/log", function(status) {
if (status === "success") {
page.evaluate(function() {
document.getElementsByName("username")[0].value = "uname";
document.getElementsByName("password")[0].value = "pass12";
document.getElementsByTagName("button")[0].click();
});
window.setTimeout(function() {
page.render("page.png");
page.open("https://website.location.com/log/#!/activity/search", function(status) {
window.setTimeout(function() {
page.render("profil.png");
console.log(page.content);
phantom.exit();
}, 5000);
});
}, 5000);
}
});
The old code worked great with the old log in page and console.log output correctly to the script that was calling this phantomjs code, but now I'm not sure where to go from here. I've tried using the page.sendEvent('keypress',page.event.key.U) in several different ways, but any time I insert that bit it seems to freeze up. When I load the page the cursor is automatically in the correct field, but otherwise I figured using tab should switch to the next field and enter would be able to enter my data.
Example of what I've tried
var page = require('webpage').create();
phantom.cookiesEnabled = true;
page.open("https://website.location.com/log", function(status) {
if (status === "success") {
page.evaluate(function() {
page.sendEvent('keypress', page.event.key.U);
page.sendEvent('keypress', page.event.key.N);
page.sendEvent('keypress', page.event.key.A);
page.sendEvent('keypress', page.event.key.M);
page.sendEvent('keypress', page.event.key.E);
page.sendEvent('keypress', page.event.key.Tab);
page.sendEvent('keypress', page.event.key.P);
page.sendEvent('keypress', page.event.key.A);
page.sendEvent('keypress', page.event.key.S);
page.sendEvent('keypress', page.event.key.S);
page.sendEvent('keypress', page.event.key.1);
page.sendEvent('keypress', page.event.key.2);
document.getElementsByTagName("button")[0].click();
});
window.setTimeout(function() {
page.render("page.png");
page.open("https://website.location.com/log/#!/activity/search", function(status) {
window.setTimeout(function() {
page.render("search.png");
console.log(page.content);
phantom.exit();
}, 5000);
});
}, 5000);
}
});
I've also tried some variations like this that don't seem any better:
page.sendEvent('keypress', "uname");
page.sendEvent('keypress', page.event.key.Tab);
page.sendEvent('keypress', "pass12");
page.sendEvent('keypress', page.event.key.Enter);