我无法通过使用phantomjs自动化来访问第二个页面[英] I cannot get access to the second page by using phantomjs automation

问题描述

我无法使用 phantomjs 自动化访问第二页显示错误类型错误:undefined 不是对象(评估 'r[20].click')未定义:7:8

在运行 phantomjs 代码时

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

推荐答案

试试这个代码:

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://www.itbaoku.cn/post/1740026.html