使用 Selenium 获取元素的位置

Get element at position with Selenium

是否可以使用 Selenium 网络驱动程序 运行 或获得 document.elementFromPoint 提供的相同功能?

为此你必须使用 JavaScript:

element = driver.execute_script("""
return document.elementFromPoint(arguments[0], arguments[1]);
""", x, y)

这假定 xy 设置为整数值。在第一个参数变为 arguments[0], arguments[1] 之后传递给 execute_script 的参数在 JavaScript 端以此类推。 (这只是很好的旧 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 如果出现 None 值:

If the specified point is outside the visible bounds of the document or either coordinate is negative, the result is null.

JavaScript null 在 Python 中变为 None