CasperjJs找不到变量:获取

时间:2019-11-16 23:35:12

标签: javascript phantomjs casperjs

我正在编写一个简单的脚本,该脚本可以在网站上登录,然后转到另一个页面并单击按钮。 似乎可以找到按钮,但是单击后出现错误:

  

页面错误:ReferenceError:找不到变量:获取

如何解决此问题?

  var casper = require('casper').create({   
        verbose: true, 
        logLevel: 'debug',
        pageSettings: {
             loadImages:  false,         // The WebPage instance used by Casper will
             loadPlugins: false,         // use these settings
             userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
        }
    });


    //print out all the messages in the headless browser context
    casper.on('remote.message', function(msg) {
        this.echo('remote message caught: ' + msg);
    });

    // print out all the messages in the headless browser context
    casper.on("page.error", function(msg, trace) {
        this.echo("Page Error: " + msg, "ERROR");
    });

    casper.on('resource.error', function(msg) {
        this.echo('resource.error caught: ' + msg);
    });



    var url = 'https://firstPage.com';

    casper.start(url, function() {

         this.evaluate(function() {
                document.getElementById("login").value = "name@email.com";
                document.getElementById("password").value = "pass";
                document.querySelector("loginButton").click();
            });

    });


    casper.thenOpen('https://secondPage.com', function() { 
         console.log("Page Title " + document.title);
      });
    casper.waitForSelector('[data-content~="OK"]', function() {
      this.click('[data-content~="OK"]');
    })

1 个答案:

答案 0 :(得分:0)

fetch不会显示为您在脚本上调用的变量或DOM元素,这很奇怪,但是在这一行中,我假设您正在调用class loginButton

document.querySelector("loginButton").click();

如果这是一个类,则应放置.(如果是ID,则应放置#),否则它将寻找不存在的loginButton元素。试试:

document.querySelector(".loginButton").click();