Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

890
Visualizações
Execute a script to get mouse position in Python & Selenium

Hello I am using selenium webdriver to get the position of the cursor in X,Y coordinates in a determined moment on the webdriver screen. I am trying a method that I found by implementing it by using diver.execute_script(), but I am having some issues with the implementation,

I am injecting the script described in the following site: https://www.dev-notes.com/blog/2008/07/30/get-current-mouse-cursor-position-with-javascript/

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


get_cursor_coordinates = """
                    window.onload = init;
                    function init() {
                        if (window.Event) {
                        document.captureEvents(Event.MOUSEMOVE);
                        }
                        document.onmousemove = getCursorXY;
                    }
                    
                    function getCursorXY(e) {
                        document.getElementById('cursorX').value = (window.Event) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
                        document.getElementById('cursorY').value = (window.Event) ? e.pageY : event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
                    }                    
                    """


self.driver.execute_script(get_cursor_coordinates)
script_vars = self.driver.execute_script('getCursorXY(null)')
            

When I execute the script in selenium to get the cursor coordinates, I get the following error:

selenium.common.exceptions.JavascriptException: Message: javascript error: getCursorXY is not defined
  (Session info: chrome=94.0.4606.61)

I would like to know how to properly execute the script with Selenium in Python in order to get the X,Y coordinates of the cursor position

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can find mouse position with pyautogui library. Install pyautogui, and then

import pyautogui

mousePosition = pyautogui.displayMousePosition()

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda