How to find the screen coordinates of a web element
4 min readJan 30, 2022
How to find the screen coordinates of a web element that Selenium can’t find.
· Motivations
· The Steps
∘ The viewport and scrolls
∘ The algorithm
∘ The code
∘ The limitations
· Conclusion
A web automation library like Selenium is a great tool for automation, but it has some limitations as well. Here are a few issues I came across after using it for a while:
- Getting a dynamically rendered object: XPath or id allows you to get static elements like buttons, text boxes, etc., but not dynamically rendered objects within an element.
- Finding screen coordinates of an element: you can get the coordinates of an element with a web browser’s viewport but not its actual screen coordinates.
- Clicking a screen point: You can click an element but not a random screen coordinate point.
- Sending a keypress event: inputting a text to an input box is no problem but not if you need to send random keypress events.
The above limitations make sense because they require interaction with the system and Selenium is designed to work within a browser. To work around these…