1. Scrolling to target element ("BROWSE TEMPLATES" button at the bottom of page) with Actions
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get('http://www.w3schools.com/')
target = driver.find_element_by_link_text('BROWSE TEMPLATES')
actions = ActionChains(driver)
actions.move_to_element(target)
actions.perform()
2. Scrolling to target element ("BROWSE TEMPLATES" button at the bottom of page) with JavaScript
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://www.w3schools.com/')
target = driver.find_element_by_link_text('BROWSE TEMPLATES')
driver.execute_script('arguments[0].scrollIntoView(true);', target)
3. Scrolling to target element ("BROWSE TEMPLATES" button at the bottom of page) with built-in method
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://www.w3schools.com/')
target = driver.find_element_by_link_text('BROWSE TEMPLATES')
target.location_once_scrolled_into_view
Note that location_once_scrolled_into_view
also returns x
, y
coordinates of element after scrolling
4. Scrolling to the bottom of page with Keys
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('http://www.w3schools.com/')
driver.find_element_by_tag_name('body').send_keys(Keys.END) # Use send_keys(Keys.HOME) to scroll up to the top of page
Note that send_keys(Keys.DOWN)
/send_keys(Keys.UP)
and send_keys(Keys.PAGE_DOWN)
/send_keys(Keys.PAGE_UP)
also could be used for scrolling