Is it possible to either run or get the same functionality provided by document.elementFromPoint
using a Selenium webdriver?
Is it possible to either run or get the same functionality provided by document.elementFromPoint
using a Selenium webdriver?
You have to use JavaScript for that:
element = driver.execute_script("""
return document.elementFromPoint(arguments[0], arguments[1]);
""", x, y)
This assumes that x
and y
are set to integer values. The arguments you pass to execute_script
after the first argument become arguments[0], arguments[1]
, etc. on the JavaScript side. (This is just the good old arguments
object. Selenium wraps the JavaScript code you give to execute_script
in a function.) The element
will either be an instance of WebElement
or None
if nothing could be found. According to the MDN page on this function a None
value will happen if:
If the specified point is outside the visible bounds of the document or either coordinate is negative, the result is
null
.
JavaScript null
becomes None
in Python.