2017-01-11 53 views
1

以下是我想要解決的html。如何發佈信息在CasperJS

enter image description here

我想發佈查詢並看到該城市的天氣報告。我想我的代碼:

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(); 

它未打印控制檯上的網絡paeg稱號。我也試過這個:

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

我沒有得到任何網頁標題也。我很困惑,並沒有做錯我所做的。此外,this.fill方法只處理與名稱屬性發布CasperJS官方網站上的信息。我需要你的幫助。

回答

1

CasperJS腳本:

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(); 

結果:

「【石家莊天氣】石家莊今天天氣預報,今天,今天天氣,7天,15天天氣預報,天氣預報一週,天氣預報15天查詢「

search.png

0

你正在做的正確,你只需要修改你的腳本。
只需修改輸入字段的選擇器實際看起來不起作用,那麼你應該得到正確的結果。
「this.fillXPath」只填寫表單,不發佈。

... 
// fill the form 
casper.waitForSelector('input', function() { 
    this.fillXPath('#txtZip', { 
     '//input[@id="txtZip"]': 'shijiazhuang' 
    }, true); 
}); 
// trigger - post the form 
casper.then(function() { 
    this.click('#btnZip'); 
}); 
// i can't speak chinese - wait for some specific change on this page 
casper.wait(5000); 
// take a screenshot of it 
casper.then(function() { 
    this.capture("shijiazhuang_weather.png"); 
}); 
...