如何使用 Python 滚动到元素可见的页面位置 Selenium.



>我目前正在使用以下代码段导航到页面中间,但它无法正常工作。

driver.execute_script("window.scrollTo(0, document.body.scrollHeight/2);")

另外,我正在尝试使用

element.location_once_scrolled_into_view

有人可以帮忙吗?

您可以在脚本中调用.scrollIntoView(),将元素作为参数传递:

driver.execute_script("arguments[0].scrollIntoView();", element)

还有move_to_element()内置的硒作用:

from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(element).perform()

差异在这里得到了完美的强调:

  • scrollIntoView vs moveToElement

最新更新