I have an ExtJs text field on a page. I am filling it with some value in casper.js, which works fine.
Then I want to focus this field and press the Enter key, as there is no <form>
around it to submit.
What I tried was:
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);
});
});
Do you have any idea how to accomplish this?
your code
evt.initKeyboardEvent('keypress', true, true, window, 0, 0, 0, 0, 0, 13);
Look at the fourth param, I guess it should be the trigger element. Here should be document.querySelector('#searchfield')
.
One hint: In casper evaluate, return a true in final, and then you will get null if there is any error in evaluate.
var result = this.evaluate(function(){ /*code is here*/ return true;})
check the result, if success
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With