casperjs使用waituntilvisible表单

时间:2015-03-30 14:18:09

标签: javascript casperjs

我使用Casperjs进行任务自动化,并且必须使用PickUp Date,DropOff Date和PickUp Location填写表格。

填写代答日期和DropOff日期工作正常。 (我可以通过截图查看)。

当您开始填充虚拟的PickUp位置时,将根据您键入的内容构建选项列表。 此列表显示在:

 <ul class="ct-autocomplete ct-ui-base" id="ui-id-1" ....></ul>

所以我尝试使用:this.waitUntilVisible('u #ui-id-1',function() 但是我收到以下错误:

[warning] [phantom] Casper.waitFor() timeout
FAIL "u#ui-id-1" never appeared in 10000ms
#    type: uncaughtError
#    error: "u#ui-id-1" never appeared in 10000ms
#    stack: not provided

感谢您的帮助 Lorenzow

casper.options.waitTimeout = 10000;

// print out all the messages in the headless browser context
casper.on('remote.message', function(msg) {
    this.echo('remote message caught: ' + msg);
});

// print out all the messages in the headless browser context
casper.on("page.error", function(msg, trace) {
    this.echo("Page Error: " + msg, "ERROR");
});

casper.start(url, function() {
   console.log("page loaded");
});

casper.then(function() {
  this.waitForResource(this.getCurrentUrl(),function() {

    //Filling Pick Up Date    
   this.click('input#ct_s1_pickup_date');
   this.fill('form#ct_s1_frm_search', {
    'ct_pickup_date':   '05/04/2015'
   },false);

   //Filling Drop Off Date
   this.click('input#ct_s1_dropoff_date');
   this.fill('form#ct_s1_frm_search', {
    'ct_dropoff_date':   '15/04/2015'
   },false);

   this.capture('screenshot step1.png');

  },function() {
  },5000);
});

casper.then(function() {

    //Filling Pick Up Location
   this.click('input#ct_s1_pickup_loc');
   this.fill('form#ct_s1_frm_search', { 
        'ct_pickup_loc':    'Barcelone'
    },false);

   this.echo(this.getHTML('ul#ui-id-1', true));

   this.waitUntilVisible('u#ui-id-1', function() {
    this.capture('screenshot step3.png');
    });
});

casper.evaluate(function(){
 });

casper.run(function() {
    this.exit();
});

1 个答案:

答案 0 :(得分:0)

“fill”看起来不会触发除“sendkeys”之外的任何事情。

所以我换了:

this.fill('form#ct_s1_frm_search', { 
        'ct_pickup_loc':    'Barcelone'
    },false);

由:

casper.then(function() { this.sendKeys('input#ct_s1_pickup_loc','Barcelone',{reset: true,keepFocus: true}); });

然后根据'Barcelone'请求构建选择列表。