python - 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 不是函数”。
我做错了什么?谢谢
解决方案
请使用下面提到的代码行而不是您正在使用的代码行:
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
推荐阅读
- c++ - 我可以将字符串存储在字符数组中但顺序相反,例如 0(index) 指向字符串中的最后一个字符吗?
- javascript - javascript - 我只能从控制台调用单击功能按钮。按钮不点击
- python - 资金流量指数关键错误
- python - Django:POST 请求时需要此字段
- java - 泛型类的反序列化问题
- c++ - 声明一个包含超过 500,000 个元素(大约)的数组会导致崩溃
- android - Android DialogFragment getDialog() 和 getView() 返回 null
- swift - SwiftUI Widget 背景基于传值的图像 url 或渐变背景
- c++ - NMAKE:致命错误 U1052:找不到文件“Makefile.vc”
- r - 从data.table r中的字符串中提取年份和星期