0

Goal: Record all these specific actions: onKeyboardEvent and onMouseEvent performed by one user on a particular website. This recording includes logging, for example, the button name, class name of that button and so on of a website only when the user clicked on that respective button. I want to create this logger with python.

Example of the expected result:

This is an illustrative example to understand what I need:

  • Event name: Mouse Left click
  • Button Name: Login
  • Class name of the button: login-button

...

  • Event name: Mouse Left click
  • Button name: Next
  • Class name of the button: next-button

Experience in python: Beginner

Python interpreter: 3.7

Question: How can I record or log the button name or class name of that button when a User clicked on it on a particular website that it opened?.

I checked Selenium and Python in order to achieve my goal. What I understood so far, selenium hat listener class and Selenium is which opens the browser and starts the event listening under that particular browser. However, in my case the user is who opens a particular website on Chrome, Firefox or Internet explorer and I need a program in python to record what the user is doing in that website.

The code attached below did not record or log anything. Besides, it did not connect to the respective browser that the user open but it listens only the session started by Selenium. Could anyone tell me if I am using the right approach or if I should use another to reach my goal?.

Thank you very much for your help and please forgive me if I used incorrectly any concept.

Python code:

import time
from selenium import webdriver
from selenium.webdriver.support.events import EventFiringWebDriver, AbstractEventListener

class MyListener(AbstractEventListener):
    def before_click(self, element, driver):
        driver.get_attribute("tag")
        print ("Event : before element click()")

    def after_click(self, element, driver):
        print("clicked on %s" %element)

driver = webdriver.Chrome('Webdrivers\Chrome_80\\chromedriver.exe')
driver = EventFiringWebDriver(driver, MyListener())
driver.get("https://google.com")
elem = driver.find_element_by_name("q")
time.sleep(20)
driver.close()
JeffC
  • 22,180
  • 5
  • 32
  • 55
sergioMoreno
  • 189
  • 1
  • 15
  • Are you talking about running a program that launches a browser and then let someone manually interact with the page and your program log these events? If so, I can pretty much guarantee you that people will interact with elements other than just buttons... what do you log then? What if the button doesn't have a name? Not all do. Not all elements will have classes either.. or the classes will change based on interaction with the page. What is exactly you are trying to accomplish? This seems like this could pretty easily be made into some kind of keylogger. What legit purpose do you have? – JeffC Feb 20 '20 at 19:49
  • Hi @JeffC, tks for your time to help with my goal. Basically I need a program to record Typing and Clicking actions made by user in a particular website. The browser is launched by user and the program must record what this user is doing there. I need to collect information about the element which user is interacting as much as I can and obviously that this information is available ( as you said not always all elements have classes). This information could be element type, element tag, element name, and so on. Hopes this explanation is better for your understanding. If not, please let me know – sergioMoreno Feb 21 '20 at 08:08

1 Answers1

0

Selenium is a python framework for testing purposes, you could see more information if you are interested in https://selenium-python.readthedocs.io/

What you need (maybe) is like a pyHook approach: https://pypi.org/project/pyHook/ It uses the mouse and keyboard events.

There is another module named pyscreenshot, it could help you for monitoring what is being clicked https://pypi.org/project/pyscreenshot/

Regards!

CCebrian
  • 75
  • 8
  • Hi @CCebrian thank you for your help. My question is how to retrieve the button name and class name of the button which was clicked by a user in a browser. As far as I know Pyhook help me to retrieve the action executed by the user but it does not give me the information that I need. Do you know how to accomplish it? – sergioMoreno Feb 20 '20 at 17:46
  • Hi @sergioMoreno, As far as I know, It is not possible to get the name and the class name a prior, for that is interesting to make a module in javascript for keylogging the user activity, in that keylogger you could log the attributes of the elements you want :) – CCebrian Feb 20 '20 at 17:52
  • Hi @CCebrian in that case you could show me withe more detail how do it please?. I do not know how to do what you say or if you could tell me a link that I can read how to build what you suggested. Thanks in advance. – sergioMoreno Feb 20 '20 at 18:19
  • 1
    This thread may help you! https://stackoverflow.com/questions/9012537/how-to-get-the-element-clicked-for-the-whole-document I see you are using Chrome so you can use this tutorial for creating one extension https://thoughtbot.com/blog/how-to-make-a-chrome-extension, good luck! – CCebrian Feb 20 '20 at 21:12
  • 1
    Hi @CCebrian, thank so much for your help. I just want to confirm whether I understood your solution correctly or not. The idea is to create the respective browser extension in order to log all the events and the information for each element that the user clicked or typed. If so, I have a further question: How can I invoke, from my python program, this extension to start listening and recieving as well the information that this extension is logging?. Anyway, very interesting both links, I am learning a lot. Thanks for your help again. – sergioMoreno Feb 21 '20 at 12:14
  • You are welcome!, yes you are in the right way, the extension is most of them in javascript, so you could send data or files to wherever you want, I don't know exactly the restrictions of the extensions, if you could not send data inside it, you can save a well formed file and then catch it remotely from your python code, parse and save it to a database. I hope I answered you right :) – CCebrian Feb 21 '20 at 12:23