最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I click this button in CasperJS? - Stack Overflow

programmeradmin2浏览0评论

After some debugging, it appears I'm having difficulty either clicking or triggering the click event of this button within CasperJS:

<a id="generate" class="btn" href="#generate"><strong>Generate</strong></a>

Here's the code I have so far:

var casper = require('casper').create({
    clientScripts: [
        '...\\JQuery\\jquery-1.11.1.min.js'
    ],
    pageSettings: {
        userAgent: 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0'
    }
});
casper.on('page.initialized', function (page) {
    page.evaluate(function () {
        (function() {
            window.screen = {
                width: 1600,
                height: 900
            };
            var fake_nav = {};
            for (var i in navigator) {
                fake_nav[i] = navigator[i];
            }
            fake_nav.javaEnabled = function() { return true; };
            fake_nav.language = 'en-US';
            window.navigator = fake_nav;
        })();
    });
});
casper.start('/', function() {
    this.echo("Loaded successfully.");
});
var template = '[\'{{repeat(1,3)}}\',    {asdf: "hello"}    ]';
casper.then(function() {
    this.evaluate(function(input) {
        window.$('.CodeMirror')[0].CodeMirror.setValue(input);
     }, template);
});
casper.then(function() {
    this.evaluate(function() {
        document.$('#generate').click();
    });
});
casper.then(function() {
    var doc = this.evaluate(function($) {
        return window.$('.CodeMirror')[1].CodeMirror.getValue();
    });
    this.echo(doc);
});
casper.run(function() {
    this.echo('All Done.');
    this.exit();
});

This generates the output (note the blank line from the "casper.echo(doc)"):

Loaded successfully.

All Done.

I've confirmed that my javascript works in the browser console in both Chrome and Firefox. Why can't I click this button in CasperJS?

After some debugging, it appears I'm having difficulty either clicking or triggering the click event of this button within CasperJS:

<a id="generate" class="btn" href="#generate"><strong>Generate</strong></a>

Here's the code I have so far:

var casper = require('casper').create({
    clientScripts: [
        '...\\JQuery\\jquery-1.11.1.min.js'
    ],
    pageSettings: {
        userAgent: 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0'
    }
});
casper.on('page.initialized', function (page) {
    page.evaluate(function () {
        (function() {
            window.screen = {
                width: 1600,
                height: 900
            };
            var fake_nav = {};
            for (var i in navigator) {
                fake_nav[i] = navigator[i];
            }
            fake_nav.javaEnabled = function() { return true; };
            fake_nav.language = 'en-US';
            window.navigator = fake_nav;
        })();
    });
});
casper.start('http://www.json-generator.com/', function() {
    this.echo("Loaded successfully.");
});
var template = '[\'{{repeat(1,3)}}\',    {asdf: "hello"}    ]';
casper.then(function() {
    this.evaluate(function(input) {
        window.$('.CodeMirror')[0].CodeMirror.setValue(input);
     }, template);
});
casper.then(function() {
    this.evaluate(function() {
        document.$('#generate').click();
    });
});
casper.then(function() {
    var doc = this.evaluate(function($) {
        return window.$('.CodeMirror')[1].CodeMirror.getValue();
    });
    this.echo(doc);
});
casper.run(function() {
    this.echo('All Done.');
    this.exit();
});

This generates the output (note the blank line from the "casper.echo(doc)"):

Loaded successfully.

All Done.

I've confirmed that my javascript works in the browser console in both Chrome and Firefox. Why can't I click this button in CasperJS?

Share Improve this question asked May 13, 2014 at 15:32 ZeeZee 1,8904 gold badges18 silver badges28 bronze badges 5
  • 2 Try that : this.click('#generate.btn'); – Fanch Commented May 13, 2014 at 16:42
  • 1 ^- This goes outside of this.evaluate and inside of casper.then. Also, I don't see why document.$ should be defined when you try to click it. Maybe also try window.$ or even $. – Artjom B. Commented May 13, 2014 at 16:58
  • @ArtjomB. I attempted document.$ , window.$ and plain $ - none worked for me. – Zee Commented May 13, 2014 at 17:13
  • Thank you so much, @Fanch - That works! Please enter that as an answer so that I can give you credit where credit is due. – Zee Commented May 13, 2014 at 17:14
  • The page might look different in casper in contrast to the browser. You can make sure the button is there see here. – Artjom B. Commented May 13, 2014 at 17:24
Add a comment  | 

2 Answers 2

Reset to default 16

Instead of using the evaluate function and click on your element in the page DOM environment (with jQuery), just use the casper method : this.click('#generate.btn');. It's easier.

There is also the clickLabel() function.

There are three functions available for clicking an element in CasperJS:

  • casper.click():
    • Performs a click on the element matching the provided selector expression.
  • casper.clickLabel():
    • Clicks on the first DOM element found containing label text.
  • casper.thenClick():
    • Adds a new navigation step to click a given selector and optionally add a new navigation step in a single operation.
发布评论

评论列表(0)

  1. 暂无评论