CasperJS数据不会出现

时间:2014-04-28 15:48:41

标签: javascript casperjs

我尝试在没有表单标签的情况下提交以下请求。 我可以看到脚本填写输入字段并提交数据但不幸的是 加载数据不会出现在html输出中。可能是自动填充问题?

Dealform标题:

<div id="js_dealform_querycenter" class="dealform_querycenter cf  dealform_homepage calendar_state_indicator">
 <div class="dealform_query_go">
 <button type="button" id="js_go" class="button search">Suchen</button>
</div>
<div class="dealform_query_input ">
<input type="text" id="js_querystring_shadow" class="querystring_shadow placeholder " autocomplete="off" 
spellcheck="false" data-default="München" value=""/>
<input type="text" id="js_querystring" class="querystring " tabindex="1" autocomplete="off" spellcheck="false"
data-pathname="München"data-pathid="3577" data-initialized="0" 
onkeydown="if(0==this.getAttribute('data-initialized')){document.getElementById('js_querystring_shadow').value=''}" value="München"/>
<div id="js_short_dealinfos" class="short_dealinfos  userdefined">
  <button class="dealinfosbutton_small">
    <span class="img_sprite_moon dealbutton_calendar"><!-- --></span>
    <span class="img_sprite_moon roomtype roomtype7"><!-- --></span>
  </button>
  <div class="dealinfosbutton hidden_phone">
    <span class="state_indicator_date_from" id="date_from">So, 18.05</span>
    <div class="state_indicator_date_divider">–</div>
      <span class="state_indicator_date_to" id="date_to">Mo, 19.05</span>
      <span class="img_sprite_moon roomtype roomtype7"></span></div>
    </div>
  </div>
</div>

Casper脚本:

casper = require('casper').create({
    waitTimeout: 20000,
    viewportSize: {
        width: 1024,
        height: 768
    },
    verbose: true,
    logLevel: 'debug',
    userAgent: 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'
});

casper.start('http://www.example.com/', function() {
//this.debugPage();
}, 7000);

casper.then(function() {
 this.evaluate(function () {
        document.getElementById('js_querystring_shadow').setAttribute('value', 'München');
        document.getElementById('js_querystring').setAttribute('value', 'München');
    document.getElementById('js_querystring').submit(); 
  });
});

casper.thenClick('#js_go').then(function() {
    require('utils').dump(this.getElementInfo('#js_querystring'));
    this.waitWhileSelector('div.cf item_wrapper', function() {
        this.echo('.selector is no more!');
            this.debugPage();
    });
});


casper.waitWhileSelector('div.cf item_wrapper', function() {
    this.echo('.selector is no more!');
});


casper.then(function() {
  this.waitForResource(this.getCurrentUrl(),function() {
        casper.capture("example.png");
  },25000);
});


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

知道如何改进这个吗?

1 个答案:

答案 0 :(得分:1)

waitForResourcewaitWhileSelector实际上是步进功能,因此您无需将它们嵌套在casper.then块中。由于您这样做,实际的调用队列将是以下代码。如果页面未通过AJAX加载或在document.onload之后构建,则可以使用casper.then代替casper.wait*

缩短:

casper.then(function() {
 this.evaluate(function () {
        document.getElementById('js_querystring_shadow').setAttribute('value', 'München');
        document.getElementById('js_querystring').setAttribute('value', 'München');
    document.getElementById('js_querystring').submit(); 
  });
});

casper.thenClick('#js_go').then(function() {
    require('utils').dump(this.getElementInfo('#js_querystring'));
    // waitWhileSelector queued later
});


casper.waitWhileSelector('div.cf item_wrapper', function() {
    this.echo('.selector is no more!');
});


casper.then(function() {
    // waitForResource queued later
});

casper.waitWhileSelector('div.cf item_wrapper', function() {
    this.echo('.selector is no more!');
    this.debugPage();
});

casper.waitForResource(this.getCurrentUrl(),function() { // this does nothing
    casper.capture("example.png");
}, 25000);