我想点击提交按钮,等待下一页加载,然后在第二页获取html.我做开始,然后运行,但是第一页仍然运行.有任何想法吗?
var casper = require('casper').create(); var site = 'http://www.example.com'; var data = {}; casper.start(site,function() { this.evaluate(function() { $('input[type="submit"]:first').click(); }); }); casper.then(function() { data.body = this.evaluate(function() { var rows = $('#content table:first tbody tr'); var listings = rows.eq(3).text(); var count = rows.eq(4).text(); return { listings: listings,count: count }; }); }); casper.run(function() { this.echo(data.body.listings); this.exit(); });
解决方法
这只是部分解决了您的问题,但您可以使用waitFor确认您已经进入第二个页面.例如:
this.waitFor(function check() { return (this.getCurrentUrl() === PAGE_2_URL); },function then() { // step to execute when check() is ok this.echo('Navigated to page 2','INFO'); },function timeout() { // step to execute if check has Failed this.echo('Failed to navigate to page 2','ERROR'); });