I cannot get access to the second page by using ph

2019-06-13 05:22发布

问题:

I cannot get access to the 2nd page by using phantomjs automation showing an error TypeError: undefined is not an object (evaluating 'r[20].click') undefined:7 :8

while running the phantomjs code

    console.log("got here");
    var page = require('webpage').create();
    page.onConsoleMessage = function(msg) {
    console.log(msg);
    };

    page.open(url, function(status) {
    if ( status === "success" ) {
        page.evaluate(function() {
            document.getElementById("txtLoginName").value = "safvan";
            document.getElementById("txtPassword").value = "safvan542";
            document.forms["logInForm"].submit();
            console.log("Login submitted!");
            var r=document.getElementsByTagName("a");
            r[20].click();
          });
        window.setTimeout(function () {
          page.render('hrtesttime.pdf');
          phantom.exit();
        }, 15000);
     }
   });

回答1:

Try this code:

console.log("got here");
var page = require('webpage').create();
page.onConsoleMessage = function(msg) {
console.log(msg);
};

page.open(url, function(status) {
if ( status === "success" ) {
    page.evaluate(function() {
        document.getElementById("txtLoginName").value = "safvan";
        document.getElementById("txtPassword").value = "safvan542";
        document.forms["logInForm"].submit();
        console.log("Login submitted!");
//After the form is submitted, wait 3s and click on the link:
        setTimeout(function(){document.getElementsByTagName("a")[20].click();},3000);
      });
    setTimeout(function(){
      page.render('hrtesttime.pdf');
      phantom.exit();
    }, 15000);
 }
});



标签: phantomjs