簡體   English   中英

casper.js:在ExtJs輸入字段中按“enter”鍵

[英]casper.js: press key “enter” in an ExtJs input field

我在頁面上有一個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中,在final中返回一個true,然后如果在evaluate中有任何錯誤,你將得到null。

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

檢查結果,如果成功

2意見建議:

第一個:嘗試this.thenEvaluate 。然后evaluate而不是evaluate 然后需要確保異步序列正常工作。

第二個:這可能是客戶端問題。 在瀏覽器中,你可以檢查js控制台,但不是在這里。 所以你應該添加一些調試助手。

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

哪個會將您的遠程控制台消息傳輸到您的控制台。 現在,您可以使用console.log()調試遠程上下文,並查看它在哪里(以及如果)中斷。

你還應該設置verbose=truelogLevel="debug"來實現這一點。

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM