Cliff
Cliff

Reputation: 11248

How to automate keypress event on web page

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?

Upvotes: 2

Views: 1364

Answers (1)

Iskandar Reza
Iskandar Reza

Reputation: 962

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()?

Upvotes: 2

Related Questions