I am trying to test keyboard input on a TV display. I can't figure out how to get Selenium web driver to work and I don't want to bring in JQuery as a dependency. Is there a way to automate keypresses in Javascript without JQuery/Selenium?
Asked
Active
Viewed 1,049 times
1 Answers
1
Maybe something like this?
let elem = document.getElementById('#myInput');
elem.dispatchEvent(new Event('focus'));
elem.dispatchEvent(new KeyboardEvent('keypress',{'key':'a'}));
And possibly use it with .setTimeout()
?

Iskandar Reza
- 953
- 1
- 7
- 16
-
That almost works but doesn't. I'm running a React app on my TV and the button I'm trying to click is managed by a react-hotkeys container which should trigger based on keyboard input but it doesn't. :( – Cliff Oct 04 '18 at 17:26
-
1document.activeElement element.dispatchEvent(new KeyboardEvent('keypress',{code:'Enter', keyCode: 13, charCode: 13, which: 13, bubbles: true, cancelBubble: false})); true – Cliff Oct 04 '18 at 17:27
-
I finally got this to work! I was misunderstanding how focus was leaving my element when I ran the code in the chrome dev tools. – Cliff Oct 17 '18 at 20:59