首页 > 解决方案 > javascript 错误:arguments[0].scrollIntoView 不是在 python 上使用 selenium 的函数

问题描述

我在 python 上使用 Selenium,我想滚动到一个元素以单击它。我到处都看到直接进入元素的正确方法是使用:

driver = webdriver.Chrome()
driver.get(url)
element = driver.find_elements_by_class_name('dg-button')
driver.execute_script("return arguments[0].scrollIntoView();", element)

但我有这个错误:“javascript 错误:arguments[0].scrollIntoView 不是函数”。

我做错了什么?谢谢

标签: pythonseleniumscroll

解决方案


请使用下面提到的代码行而不是您正在使用的代码行:

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

更新的答案:
您也可以使用location_once_scrolled_into_view它给出元素的坐标,但它也会将元素滚动到视图中。你可以像这样使用它:

element = driver.find_elements_by_class_name('dg-button')
element.location_once_scrolled_into_view

推荐阅读