这是我的方法。因为Web上下文包含jQuery,所以我们可以使用它来触发事件,并且重要的一步是,在每个ajax调用之后,我们必须等待并验证,然后才能处理任何下一步。
//set select valuesvar optionFirstSelect = 125;var optionSecondSelect = 6; var optionThirdSelect = 47;//create casper objectvar casper = require('casper').create({ loadImages:false, verbose: true, logLevel: 'debug'});//open urlcasper.start('http://thewebsite.com');casper.then(function(){ //select option on first select this.evaluate(function(valueOptionSelect){ $('select#s1').val(valueOptionSelect); $('select#s1').trigger('change'); },optionFirstSelect); //wait until the second select is populated this.waitFor(function check() { return this.evaluate(function() { return document.querySelectorAll('select#s2 option').length > 1; }); }, function then() { //select option on second select this.evaluate(function(valueOptionSelect){ $('select#s2').val(valueOptionSelect); $('select#s2').trigger('change'); },optionSecondSelect); //wait until the third select is populated this.waitFor(function check() { return this.evaluate(function() { return document.querySelectorAll('select#s3 option').length > 1; }); }, function then() { //select option on third select this.evaluate(function(valueOptionSelect){ $('select#s3').val(valueOptionSelect); $('select#s3').trigger('change'); },optionThirdSelect); //wait until table with info is populated after an option is seleted on third select. this.waitFor(function check() { return this.evaluate(function() { return document.querySelectorAll('table#info tbody tr').length > 1; }); }, function then() { //Do verifications here ... }); }); }); });casper.run(function() { //finish execution script this.exit();});


