在Selenium Python中查找视口的位置?

2024-05-14 14:51:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在Selenium中找到视口左上角的位置。我知道我们可以使用drive_get_window_position()来获取窗口位置,但我只需要渲染部分的位置。可能吗

用例是,我试图使用Selenium获取元素的坐标,并且需要将光标移动到元素上。由于Selenium只移动虚拟鼠标而不移动可见光标,因此我使用pyautogui将光标移动到绝对坐标

但是,由于浏览器位置会影响绝对坐标的位置,因此我需要知道视口的位置,以便进行适当的偏移。由于各种菜单和工具栏,使用get_window_position()可以处理x坐标,但不能处理y坐标


Tags: 元素getselenium菜单浏览器positiondrive鼠标
1条回答
网友
1楼 · 发布于 2024-05-14 14:51:31

location_once_scrolled_into_view¶

THIS PROPERTY MAY CHANGE WITHOUT WARNING. Use this to discover where on the screen an element is so that we can click it. This method should cause the element to be scrolled into view.

https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webelement.html#module-selenium.webdriver.remote.webelement

返回屏幕上的左上角位置,如果元素不可见,则返回“无”

例如:

e = driver.find_element_by_xpath('//html')
print(e.location_once_scrolled_into_view)

相关问题 更多 >

    热门问题