casper.js:在ExtJs输入字段中按"enter"键

Dan*_*Lee 5 javascript forms keypress extjs2 casperjs

我在页面上有一个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);
  });
});
Run Code Online (Sandbox Code Playgroud)

你知道怎么做到这一点吗?

alo*_*ser 0

2条建议:

第一:尝试this.thenEvaluate代替evaluate. then 需要确保异步序列正常工作。

第二:这可能是客户端 js 问题。在浏览器中你可以只检查 js 控制台,但不能在这里。所以你应该添加一些调试助手。

casper.on('log.message', function(msg){
  console.log(msg);
}
Run Code Online (Sandbox Code Playgroud)

这会将您的远程控制台消息传输到您的控制台。现在您可以调试远程上下文并console.log()查看它在哪里(以及是否)中断。

您还应该设置verbose=truelogLevel="debug"使其起作用。

祝你好运!