CasperjJs Can't find variable: fetch

407 Views Asked by At

I am writing a simple script that login on website and then go to another page and click button. It seems like it can find button, but there is an error after click:

Page Error: ReferenceError: Can't find variable: fetch

How can I resolve this issue?

  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 = "[email protected]";
                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

There are 1 best solutions below

0
MNN On

fetch doesn't appears as a variable or a DOM element that you call on the script, that's strange, but here on this line I assume that you're calling a class loginButton

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

If it's a class you should put the . (or # if it's an ID), other way it will be looking for an loginButton element that doesn't exists. Try:

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