我在页面上有一个ExtJs文本字段。我在casper.js中填充了一些值,效果很好。
然后,我要关注此字段并按Enter键,因为周围没有<form>可以提交。

我试过的是:

casper.then(function() {
  // the text field is filled with the string
  this.sendKeys('#searchfield', 'some text');

  this.evaluate(function() {
    // this does not put the field in focus
    document.querySelector('#searchfield').focus();

    // so 'pressing' enter has no effect at all
    var evt = document.createEvent('KeyboardEvent');
    evt.initKeyboardEvent('keypress', true, true, window, 0, 0, 0, 0, 0, 13);
    document.dispatchEvent(evt);
  });
});

你有什么想法要完成吗?

最佳答案

您的代码

evt.initKeyboardEvent('keypress', true, true, window, 0, 0, 0, 0, 0, 13);

看第四个参数,我想它应该是触发元素。这应该是document.querySelector('#searchfield')

一个提示:在casper评估中,最后返回true,然后如果评估中有任何错误,您将得到null。
var result = this.evaluate(function(){ /*code is here*/ return true;})

检查结果,如果成功

07-24 09:43
查看更多