Last modified: Dec 01, 2023 By Alexander Williams

Python Selenium: Scroll to Element - Examples

Example 1: Scroll to Element using JavaScript Executor


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage with a target element
driver.get('https://example.com')
target_element = driver.find_element_by_id('target-element')

# Use JavaScript Executor to scroll to the target element
driver.execute_script("arguments[0].scrollIntoView();", target_element)

# Close the browser
driver.quit()

Output:


# The webpage scrolls to the target element

Example 2: Scroll to Element using Action Chains


from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage with a target element
driver.get('https://example.com')
target_element = driver.find_element_by_id('target-element')

# Use Action Chains to move to the target element
actions = ActionChains(driver)
actions.move_to_element(target_element).perform()

# Close the browser
driver.quit()

Output:


# The webpage scrolls to the target element

Example 3: Scroll to Element with Offset using JavaScript Executor


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage with a target element
driver.get('https://example.com')
target_element = driver.find_element_by_id('target-element')

# Use JavaScript Executor to scroll to the target element with an offset
offset = -100  # Adjust the offset as needed
driver.execute_script(f"arguments[0].scrollIntoView({{ behavior: 'smooth', block: 'center', inline: 'center' }});", target_element)

# Close the browser
driver.quit()

Output:


# The webpage scrolls to the target element with an offset

Example 4: Scroll to Element Using Keys


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage with a target element
driver.get('https://example.com')
target_element = driver.find_element_by_id('target-element')

# Use Keys to scroll to the target element
target_element.send_keys(u'\ue007')  # Press Enter to scroll into view

# Close the browser
driver.quit()

Output:


# The webpage scrolls to the target element

Example 5: Scroll to Element Using ScrollIntoViewIfNeeded


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage with a target element
driver.get('https://example.com')
target_element = driver.find_element_by_id('target-element')

# Use JavaScript Executor to scroll to the target element with ScrollIntoViewIfNeeded
driver.execute_script("arguments[0].scrollIntoViewIfNeeded(true);", target_element)

# Close the browser
driver.quit()

Output:


# The webpage scrolls to the target element using ScrollIntoViewIfNeeded

Example 6: Scroll to Element Using ScrollTop


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage with a target element
driver.get('https://example.com')
target_element = driver.find_element_by_id('target-element')

# Use JavaScript Executor to scroll to the target element with ScrollTop
driver.execute_script(f"window.scroll(0, {target_element.location['y']});")

# Close the browser
driver.quit()

Output:


# The webpage scrolls to the target element using ScrollTop