liubochao988
liubochao988

Reputation: 19

how can casperjs click() tag not an anchor

target html is like

<script src="http://static.com/js/qiyi/config.js" type="text/javascript">
<dl class="selected">
   <dt>
       <span data-value="2014">2014年</span>
   </dt>
</dl>
<dl>
   <dt>
      <span data-value="2013">2013年</span>
   </dt>
</dl>

is not a simple anchor tag, so in my browser, when i click the span tag, it will call the config.js to do something(i dont know what does it did), it will get change the content. but when i use

this.click()

to simulate the mouse action, it doesnot work. because i know the casperjs can work like a browser. if i want to crawl different years content, how can i do

my code is

var casper = require("casper").create({
    clientScripts: [
        "jquery-1.7.2.js",
        "config.js"],  //in url page call this js
    remoteScripts: ["http://static.qiyi.com/js/qiyi/config.js"],//also the config.js

    stepTimeout: 120 * 1000,  //单步超时时间
    pageSettings: {  
        loadImages: false  
    },  
    verbose: true,  
    logLevel: "error"  
});   

var fs = require('fs');
var filename = 'content.txt';

casper.on('resource.received', function(resource) {
    casper.echo(resource.url);
});

casper.on('click', function(resource) {
    casper.echo("click");
});

casper.on("page.error", function(msg, trace) {
    this.echo("Error: " + msg, "ERROR");
});

casper.on("resource.error", function(resourceError {
    console.log(JSON.stringify(resourceError));});

casper.start("http://www......");
casper.then(function() {
    this.evaluate(function(){
        document.querySelectorAll('[data-value="2013"]')[0].click();
        //it does not work
    });
    this.echo("click");
});
casper.then(function() {
    this.click('span[data-value="2013"]');
    //it does not work
    fs.write(filename, this.getHTML(), 'w');
    this.echo("click");
});

casper.run();

Upvotes: 0

Views: 1038

Answers (3)

liubochao988
liubochao988

Reputation: 19

if you first use js, you must be sure about how to debug,include page.error and capture event is important.like

casper.on("click", function(){this.echo();});
casper.on("page.error", function(){this.echo();});

the event happen you can get. for this problem, after click, must have some time to receive the resource.

this.wait(5000, function(){fs.write(...);})

thank you @ArtjomB.

Upvotes: 1

Ka0s
Ka0s

Reputation: 398

Use the clickLabel function, outside of the evaluate function Works for span's in my code

casper.then(function(){
    this.clickLabel('2014', 'span');
});

Found here

Upvotes: 0

bob_cobb
bob_cobb

Reputation: 2269

You probably want to evaluate some javascript (that has access to the window object as if you were in the browser) with casper.evaluate

casper.evaluate(function() {
  document.querySelectorAll('[data-value="2014"]')[0].click();
});

Upvotes: 0

Related Questions