javascript error: arguments[0].scrollIntoView is not a function using selenium on python

廉价感情. 提交于 2021-02-05 09:31:45

问题


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)

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

What to I do wrong ? Thanks


回答1:


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

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

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



回答2:


scrollIntoView() is part of the DOM API and you need to run it on an WebElement but not on a List of WebElement(s).

You need to change find_element(s) to find_element:

element = driver.find_element_by_class_name('dg-button')
driver.execute_script("return arguments[0].scrollIntoView();", element)


来源:https://stackoverflow.com/questions/55492695/javascript-error-arguments0-scrollintoview-is-not-a-function-using-selenium-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!