Search code examples
casperjs

How to post information in CasperJS


The following is the html I wanna tackle with.

enter image description here

I wanna post a query and see the weather reports for that city. I tried my codes:

var casper = require('casper').create();
var utils = require('utils');

casper.start('http://www.weather.com.cn/weather1d/101210101.shtml'); 
casper.waitForSelector('input', function()
{
    this.fillXPath('div.search clearfix',{'//input[@id="txtZip"]':'shijiazhuang'},true);
}); 
casper.then(function()
{   
    utils.dump(this.getTitle());
}); 
casper.run();

It did not print the web paeg title on the console. I also tried this:

casper.waitForSelector('input', function()
{
    this.fillSelectors('div.search clearfix',{'input[id="txtZip"]':'shijiazhuang'},true);
}); 
}
);

I did not get any web page title also. I got quite confused and did not what I had done wrong. Besides, it seems that this.fill method only tackles with name attributes to post information on CasperJS's official website. I need your help.


Solution

  • CasperJS script:

    var casper = require('casper').create(), utils = require('utils');
        casper
    
    .start('http://www.weather.com.cn/weather1d/101210101.shtml',function(){
        this
             .wait(3000,function(){
        this.capture('search.png');
        utils.dump(this.getTitle());
    })
             .evaluate(function(){
                document.getElementById('txtZip').value='shijiazhuang';
                document.querySelector('input[type="button"]').click();
           })
    })
        .run();
    

    Result:

    "【石家庄天气】石家庄今天天气预报,今天,今天天气,7天,15天天气预报,天气预报一周,天气预报15天查询"

    search.png