0

I use selenium python, and i want to get Coordinates whenever i click any point this is my code, anybody help me, how to get result in python

result = driver.execute_script("""
    function clickHandler(e) {
        console.log(e)
        var clickCoordinates = e.clientX + ',' + e.clientY;
        console.log(clickCoordinates)
        return clickCoordinates
    }

    document.addEventListener('click', clickHandler);
""")
print(result)

I want to print clickCoordinates whenever click event

1 Answers1

0

MouseEvent clientX Property

The clientX is a read-only property of the MouseEvent interface which provides the horizontal coordinate within the application's viewport at which the event occurred (instead of the coordinate within the page).

As an example to retrieve the clientX coordinate of the mouse pointer while the mouse pointer moves you can use:

let x = event.clientX;

MouseEvent clientY Property

The clientY is a read-only property of the MouseEvent interface which provides the vertical coordinate within the application's viewport at which the event occurred (instead of the coordinate within the page).

As an example to retrieve the clientY coordinate of the mouse pointer while the mouse pointer moves you can use:

let y = event.clientY;

This usecase

To print clickCoordinates whenever click event occurs you can use the following solution:

result = driver.execute_script("""
    function clickHandler(e) {
    console.log(e)
    console.log(e.clientX)
    console.log(e.clientY)
    var clickCoordinates = e.clientX + ',' + e.clientY;
    console.log(clickCoordinates)
    return clickCoordinates
    }

    document.addEventListener('click', clickHandler);
""")
print(result)

References

Link to useful documentations:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352