Scrolling to element using webdriver?

Question:

I am still learning and in response to one of my questions: here, I was told to that it might be due because the element in question is not in view.

I looked through the documentation and SO, here was the most relevant answer: here

You can use the “org.openqa.selenium.interactions.Actions” class to move to an element:

WebElement element = driver.findElement(By.id("my-id"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
## actions.click();
actions.perform();

When I try to use the above to scroll to the element:
It says WebElement not defined.

I think this is because I have not imported the relevant module. Can someone point out what I am supposed to import?

Edit:
As pointed out by alecxe, this was java code.

But in the meantime right after trying to figure it out for some time. I have found out the import method for WebElement:

from selenium.webdriver.remote.webelement import WebElement

Might help someone like me.

The how of it is also a good lesson, IMO:

Went to: Documentation
The

class selenium.webdriver.remote.webelement.WebElement(parent, id_, w3c=False)

Need to be separated into the command form mentioned above.

Asked By: Sid

||

Answers:

You are trying to run Java code with Python. In Python/Selenium, the org.openqa.selenium.interactions.Actions are reflected in ActionChains class:

from selenium.webdriver.common.action_chains import ActionChains

element = driver.find_element_by_id("my-id")

actions = ActionChains(driver)
actions.move_to_element(element).perform()

Or, you can also “scroll into view” via scrollIntoView():

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

If you are interested in the differences:

Answered By: alecxe

It’s not a direct answer on question (its not about Actions), but it also allow you to scroll easily to required element:

element = driver.find_element_by_id('some_id')
element.location_once_scrolled_into_view

This actually intend to return you coordinates (x, y) of element on page, but also scroll down right to target element

Answered By: Andersson

There is another option to scroll page to required element if element has "id" attribute

If you want to navigate to page and scroll down to element with @id, it can be done automatically by adding #element_id to URL…

Example

Let’s say we need to navigate to Selenium Waits documentation and scroll page down to “Implicit Wait” section. We can do

driver.get('https://selenium-python.readthedocs.io/waits.html')

and add code for scrolling…OR use

driver.get('https://selenium-python.readthedocs.io/waits.html#implicit-waits')

to navigate to page AND scroll page automatically to element with id="implicit-waits" (<div class="section" id="implicit-waits">...</div>)

Answered By: Andersson

You can scroll to the element by using javascript through the execute_javascript method.
For example here is how I do it using SeleniumLibrary on Robot Framework:

web_element = self.selib.find_element(locator)
self.selib.execute_javascript(
    "ARGUMENTS",
    web_element,
    "JAVASCRIPT",
    'arguments[0].scrollIntoView({behavior: "instant", block: "start", inline: "start"});'
)
Answered By: Antony Fuentes

In addition to move_to_element() and scrollIntoView() I wanted to pose the following code which attempts to center the element in the view:

desired_y = (element.size['height'] / 2) + element.location['y']
window_h = driver.execute_script('return window.innerHeight')
window_y = driver.execute_script('return window.pageYOffset')
current_y = (window_h / 2) + window_y
scroll_y_by = desired_y - current_y

driver.execute_script("window.scrollBy(0, arguments[0]);", scroll_y_by)
Answered By: Neil C. Obremski

This can be done using driver.execute_script():-

driver.execute_script("document.getElementById('myelementid').scrollIntoView();")
Answered By: ePandit

Example:

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

This one always works for me for any type of selectors.
There is also the Actions class, but for this case, it is not so reliable.

Answered By: vitaliis