我正在尝试用javascript模拟'enter'按键以实现自动化.
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;
Run Code Online (Sandbox Code Playgroud)
这是用于设置键事件的代码(我也尝试过keydown和keyup).
在搜索Google时,这似乎不起作用.如果我输入一些文本并在输入字段上触发事件则$("[name=q]").trigger(e)没有任何反应.
我正在使用谷歌来测试模拟"正确"的输入事件.我希望使用js来自动化skype web客户端.
有谁知道是否可以使用javascript模拟实际的输入按键?我已经看到了Selenide的pressEnter()作品,但它使用的是webdriver所以也许它不相关.
我也尝试过本机js事件触发
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, '');
Run Code Online (Sandbox Code Playgroud)
sidenote我知道可以通过调用元素上的.submit()来提交查询,但这不是我所追求的.