Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect user key/mouse in Python Selenium

I'm using Selenium Browser for day to day browsing, and I'd like to fire some code when I press some keys on any page. At first I thought I can just load javascript on every page that registers keys/mouse input, but I'd actually really prefer to have some python list available with past keys/mouse clicks, e.g. my key example in javascript:

var myhistory = []

document.addEventListener("keydown", keyDownTextField, false);

function keyDownTextField(e) {
var keyCode = e.keyCode;
  myhistory.push(keyCode)
}

Is there any way to do this in pure Python/Selenium?

like image 222
PascalVKooten Avatar asked Sep 29 '22 14:09

PascalVKooten


1 Answers

What I would try:

  1. Execute a javascript that registers at the document body

    <body onkeyup="my_javasctipt_keyup()" and onkeydown="my_javasctipt_keydown()"> 
    

    using browser.execute_script. (partially solved, see question)

  2. Save the key up and keydown events in a variable in javascript. (solved, see question)

  3. use browser.execute_script to return the variables.

What I am uncertain about:

  • The return value of browser.execute_script may return json serializable objects or strings only
  • keyup and keydown in body may not work if they are used in child elements that define their own event listeners

Hopefully this is of help. If any code results form this I would be interested in knowing.

This code is what I feel should work:

from selenium import webdriver
browser = webdriver.Firefox()
browser.execute_script("""var myhistory = []

document.addEventListener("keydown", keyDownTextField, false);

function keyDownTextField(e) {
var keyCode = e.keyCode;
  myhistory.push(keyCode)
}""")
def get_history():
    return browser.execute_script("myhistory")

# now wait for a while and type on the browser
import time; time.sleep(5000)

print("keys:", get_history())

The point is that the code of selenium can never run at the same time as the browser handles keyboard input. As such, events need to be handled in javascript, the result saved, e.g. in an array and then, when selenium is asked, the array is returned to Python.

like image 172
User Avatar answered Oct 03 '22 10:10

User