I'm trying to simulate an 'enter' keypress with javascript for automation.
var script = document.createElement('script');
script.src = 'https://code.jquery.com/jquery-1.10.2.min.js';
script.type = 'text/javascript';
document.body.appendChild(script);
var e = jQuery.Event("keypress");
e.which = 13; //choose the one you want
e.keyCode = 13;
This is the code used to setup the key event (I've tried keydown and keyup as well).
This doesn't seem to work when searching Google. If I type some text and trigger the event on the input field $("[name=q]").trigger(e)
nothing happens.
I'm using google to test simulating a "proper" enter event. I hope to use js to automate skype web client.
Does anyone know if it is possible to simulate an actual enter keypress using javascript? I've seen that Selenide's pressEnter()
works but it uses webdriver so maybe it's not relevant.
I've also tried native js event triggering
var dispatchKeyboardEvent = function(target, initKeyboradEvent_args) {
var e = document.createEvent("KeyboardEvents");
e.initKeyboardEvent.apply(e, Array.prototype.slice.call(arguments, 1));
target.dispatchEvent(e);
};
dispatchKeyboardEvent($("[name=q]"), 'keypress', true, true, null, 'h', 13, '');
sidenote I am aware that query can be submitted by calling .submit() on the element but that's not what I'm after.