【问题标题】:casper.js: press key "enter" in an ExtJs input fieldcasper.js:在 ExtJs 输入字段中按“输入”键
【发布时间】:2013-06-01 07:37:45
【问题描述】:

我在页面上有一个 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);
  });
});

你知道如何完成这个吗?

【问题讨论】:

    标签: javascript forms keypress casperjs extjs2


    【解决方案1】:

    2 条建议:

    第一个:尝试this.thenEvaluate 而不是evaluate。然后需要确保异步序列正常工作。

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

    casper.on('log.message', function(msg){
      console.log(msg);
    }
    

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

    您还应该设置 verbose=truelogLevel="debug" 以使其正常工作。

    祝你好运!

    【讨论】:

    • 感谢您的回答并为我的延误感到抱歉。我认为问题不在于 casper 级别,但它甚至不在普通浏览器窗口(Chrome)中。
    【解决方案2】:

    你的代码

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

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

    提示:在casper evaluate中,在final中返回一个true,然后如果evaluate有错误,则返回null。

    var result = this.evaluate(function(){ /*code is here*/ return true;})
    

    检查结果,如果成功

    【讨论】:

    • 感谢您的回复。第四个参数是viewArg,通常只是window 对象。不幸的是,将其更改为我的元素并没有改变任何东西。它只是没有被按下。
    • 我现在只用\n发送回车键,试试这个this.sendKeys('#searchfield', 'some text\n');
    猜你喜欢
    • 1970-01-01
    • 2015-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 2018-09-23
    • 2019-01-19
    相关资源
    最近更新 更多