How to adjust variability between Selenium element coordinates and PyAutoGUI coordinates?

Viewed 39

I'm trying to move my mouse pointer to a certain element on the webpage. I used elem = driver.find_element_by_xpath(xpath) to locate the element, and elem.location to get its x, y coordinates. However, when I use PyAutoGUI to move my mouse pointer to those coordinates, they end up somewhere else. It appears that the location coordinates given by Selenium are not really the on-screen coordinates.

Upon further probing, I got the following results:

driver.maximize_window()
print("Window size (Selenium):", driver.get_window_size())
print("Window position (Selenium):", driver.get_window_position())
print("Window size (PyAutoGUI):", pyautogui.size())

Output:

Window size (Selenium): {'width': 1552, 'height': 840}
Window position (Selenium): {'x': -8, 'y': -8}
Window size (PyAutoGUI): Size(width=1920, height=1080)

This again indicates that on-screen coordinates are different from what selenium returns.

I'm aware of selenium webdriver's move_to_element method, which gets us directly to the element's location, but it doesn't actually move the on-screen mouse pointer. In the end, I'm looking for a way to actually move the mouse pointer to any given element. Any help is appreciated. Thanks!

0 Answers
Related