admin管理员组文章数量:1330156
I'm trying to simualte keyboard events in webpage through javascript since Actions is not supported in safari browser.
To Start with I created a simple form (given below) and trying to tab through the text boxes but it didn't work.
Java script used: (ubuntu and chrome browser). I fired the script in the chrome browser console.
var pressTabKey = document.createEvent("KeyboardEvent");
pressTabKey.initKeyboardEvent("keypress", true, true, null, false, false, false, false, 9, 0);
document.getElementById('1234').focus();
document.getElementById('1234').dispatchEvent(pressTabKey);
HTML Form:
<html>
<head>
</head>
<body>
<p>Test Page </p>
<form>
<input id="1234" type="text" value="Enter Here">
<br>
<br>
<input id="1235" type="text" value="Enter Here">
</form>
</body>
</html>
I'm trying to simualte keyboard events in webpage through javascript since Actions is not supported in safari browser.
To Start with I created a simple form (given below) and trying to tab through the text boxes but it didn't work.
Java script used: (ubuntu and chrome browser). I fired the script in the chrome browser console.
var pressTabKey = document.createEvent("KeyboardEvent");
pressTabKey.initKeyboardEvent("keypress", true, true, null, false, false, false, false, 9, 0);
document.getElementById('1234').focus();
document.getElementById('1234').dispatchEvent(pressTabKey);
HTML Form:
<html>
<head>
</head>
<body>
<p>Test Page </p>
<form>
<input id="1234" type="text" value="Enter Here">
<br>
<br>
<input id="1235" type="text" value="Enter Here">
</form>
</body>
</html>
Share
Improve this question
asked Sep 13, 2015 at 8:02
user1925406user1925406
7334 gold badges15 silver badges34 bronze badges
5
- Isn't it a duplicate of stackoverflow./questions/961532/…? – Capsule Commented Sep 16, 2015 at 3:33
- I did go through that thread and used those functions and parameters. But I couldn't simulate keyboard events like tabbing eventhough I could simulate mouse events. Anyway, I would checks those two blogs again. – user1925406 Commented Sep 16, 2015 at 4:08
- Maybe this is of help: stackoverflow./questions/596481/… – Jos Commented Sep 21, 2015 at 15:21
- When I tried the code you showed above - jsfiddle/pymgnbk9/2 - it tabbed to the first input field as expected, so not sure what is wrong. Did you make sure the HTML loaded before calling the JS? – jjbskir Commented Sep 22, 2015 at 15:58
-
Sorry, why can't you just do the jumps with
focus()
? jsfiddle/pymgnbk9/3 – webdeb Commented Sep 23, 2015 at 0:33
3 Answers
Reset to default 3 +25Hope this helps:
https://developer.mozilla/en-US/docs/Web/Guide/Events/Creating_and_triggering_events
Example: I create an event and use dispatchEvent() to trigger it:
var pressTabKey = new Event('keydown');
document.getElementById('1234').addEventListener('keydown', function() { alert("hi!"); });
document.getElementById('1234').dispatchEvent(pressTabKey);
The function createEvent() is deprecated: https://developer.mozilla/en-US/docs/Web/API/Document/createEvent
EDIT:
You could simply read the key code from a keydown or keypress event, like so:
http://jsfiddle/adrielD/me9q1qu6/
HTML
<p>Test Page </p>
<form>
<input id="1234" type="text" value="Enter Here"><br>
<input id="1235" type="text" value="Enter Here">
</form>
JS:
var tab1 = document.getElementById("1234");
var tab2 = document.getElementById("1235");
tab1.addEventListener("keydown", function(event) {
if(event.keyCode == 9) {
event.preventDefault();
tab2.focus();
}
});
tab2.addEventListener("keydown", function(event) {
if(event.keyCode == 9) {
event.preventDefault();
tab1.focus();
}
});
If you happen to have jQuery loaded, you can do it like this:
$(el).trigger({type: 'keypress', which: 13, keyCode: 13});
Which for your example would be:
$("#1234").trigger({type: 'keypress', which: 9, keyCode: 9});
See http://api.jquery./trigger/ for the full documentation.
I have met exactly the same problem, tried many things, and end up using AHK to trigger the keyboard event (and some mouse events). I just cannot find any selenium-only solution. AHK is for Windows only, though. If you are using selenium on other platforms, like Mac OS, check out this thread.
With AHK, it is fairly easy to do a key press, after setting the focus, just start the AHK script, in which you 'send the key':
send, abc
AHK has a full-blown scripting language, so you can wrap this in a loop/conditional. You can also simulate mouse clicks at a specific position, if necessary.
本文标签: selenium webdriverSimulating keyboard event through javascriptStack Overflow
版权声明:本文标题:selenium webdriver - Simulating keyboard event through javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742221908a2435499.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论