javascript 错误:arguments[0].scrollIntoView 不是在 python 上使用 selenium 的函数 [英] javascript error: arguments[0].scrollIntoView is not a function using selenium on python

查看:798
本文介绍了javascript 错误:arguments[0].scrollIntoView 不是在 python 上使用 selenium 的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 python 上使用 Selenium,我想滚动到一个元素来点击它.在我看到的任何地方,直接转到元素的正确做法是使用 :

I'm using Selenium on python and I would like to scroll to an element to click on it. Everywhere I see that the rigth things to do to go directly to the element is to use :

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 不是一个函数".

But I have this error : "javascript error: arguments[0].scrollIntoView is not a function".

我做错了什么?谢谢

推荐答案

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

Please use the line of code mentioned below instead of the one you are using:

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

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

Updated answer:
You can also use location_once_scrolled_into_view it gives the coordinates of the element but it does scrolls the element into view as well. You can use it like:

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

这篇关于javascript 错误:arguments[0].scrollIntoView 不是在 python 上使用 selenium 的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆