Search code examples
pythonseleniumselenium-webdriverscrollselenium-chromedriver

How to “scroll down” some part using selenium in python?


Hope you are good I m trying to make an simple script but I got stuck on there I am trying to scroll the list to get more but i am unable to scroll down. Anybody have an idea how is that done.

here is my code:

import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from shutil import which
import time
import pandas as pd
import json
# from fake_useragent import UserAgent
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
chrome_path = which('chromedriver')
driver = webdriver.Chrome(executable_path=chrome_path)
driver.maximize_window()

driver.get('http://mapaescolar.murciaeduca.es/mapaescolar/#')

driver.find_element_by_xpath('//ul[@class="nav property-back-nav floating-box pull-right"]//button').click()
time.sleep(3)
driver.find_element_by_xpath('//button[@ng-click="openCloseFilters()"]').click()
time.sleep(3)
driver.find_element_by_xpath('//select[@title="Enseñanza"]/option[1]').click()



element = driver.find_element_by_xpath('//div[@id="container1"]')
driver.execute_script("return arguments[0].scrollIntoView(true);", element)

And the list I want to scroll down :

This is list i want to scroll


Solution

  • Because the scroll is actually inside an element, all the javascript command with window. will not work. Also the element is not interactable so Key down is not suitable too. I suggest using scrollTo javascript executor and set up a variable which will increase through your loop:

    element = driver.find_element_by_xpath('//div[@id="container1"]')
    time.sleep(10)
    
    verical_ordinate = 100
    for i in range(0, 50):
       print(verical_ordinate)
       driver.execute_script("arguments[0].scrollTop = arguments[1]", element, verical_ordinate)
       verical_ordinate += 100
       time.sleep(1)
    

    I have tested with chrome so it should work.

    Reference

    https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop