7

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

9
  • 1
    change to driver.execute_script("arguments[0].scrollIntoView();", element) and let me know Commented Apr 3, 2019 at 10:20
  • I have the exact same error Commented Apr 3, 2019 at 10:22
  • are you importing from selenium import webdriver this ? Commented Apr 3, 2019 at 10:25
  • 1
    What browser are you using? try update it and the driver. Commented Apr 3, 2019 at 10:25
  • I'm using this from selenium import webdriver on python 2.7 Commented Apr 3, 2019 at 10:31

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

@JulienThillard have updated my answer, please check the updated answer
Ok I was going to a wrong element... and this one working driver.execute_script("arguments[0].scrollIntoView();", element)
3

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)

Comments

1

If you need to use it on a List of elements, than you can override it with Python, so you can navigate to the element that you want

e.g. first element of a List

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

e.g. last element of a List

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

Hope that it helps :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.