the JS in evaluate function can't been executed

72 Views Asked by At

I'm new here.When I try to use casperjs.evaluate() to execute the JS,I find the events were not executed. The code like

    casper = require("casper").create();

    casper.start("https://www.google.com/",function(){
        this.echo(this.getTitle());
        this.wait(1000);
        this.capture("homepage.png")
    });
    casper.then(function(){
        this.evaluate(function(){
            document.getElementsByClassName("gLFyf gsfi").value = "google"
        })
        this.echo(this.getTitle())
        this.wait(1000)
        this.capture("input.png")
    });

    casper.then(function(){
        var tmp = this.evaluate(function(){
            document.getElementsByClassName('gNO89b')[1].click();
        });
        this.echo(this.getTitle())
        casper.wait(1000);
        casper.capture("searchList.png");
    });


    casper.run();

the events like "value" and "click" should been executed in my expectation,so the "searchList.png" should show all the search result of "google",but in fact I got nothing. By the way,I'm sure the JS in "evaluate" function is correct.

Could anybody help to tell me why it happened ? Thanks in advance!

1

There are 1 best solutions below

0
MNN On

the document.getElementsByClassName() method returns an array as a result. So, if you want to fetch the first DOM element with gLFyf gsfi class (even if there is only one) you should call it this way:

document.getElementsByClassName("gLFyf gsfi")[0].value = "google"