I cannot get access to the second page by using phantomjs automation

巧了我就是萌 提交于 2019-12-08 10:21:48

问题


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);
 }
});



来源:https://stackoverflow.com/questions/41423699/i-cannot-get-access-to-the-second-page-by-using-phantomjs-automation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!