Skip to content Skip to sidebar Skip to footer

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 i

Solution 1:

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 = 100for i in range(0, 50):
   print(verical_ordinate)
   driver.execute_script("arguments[0].scrollTop = arguments[1]", element, verical_ordinate)
   verical_ordinate += 100time.sleep(1)

I have tested with chrome so it should work.

Reference

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

Solution 2:

Simply change the script to arguments[0].scrollTop = arguments[0].scrollHeight. You can call that script in some loop with a timeout to continuously fetch more data (that is continuously scrolling the div to bottom)

Example:

while True:
    time.sleep(1)
    driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", element)
    ifno_new_data_available():
        break

Solution 3:

Try this :

    SCROLL_PAUSE_TIME = 0.5# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

whileTrue:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height

Or simply select your element in tis case the div containing the scroll view and type this :

label.sendKeys(Keys.PAGE_DOWN);

Solution 4:

# Use this line in a loop, accordingly how much screen to be scrolled down# this just scrolls down to the height of browser screen, hence loop.

driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
# Now if the website needs time to load its data after scroll, add this in the loop..

time.sleep(5)

Solution 5:

In your case that there is a list of items, so you can follow this method :

for c inrange(1, 12): # check the list start from 0 or 1 
    time.sleep(5)  # Give time to loading the information
    element = driver.find_element_by_xpath(f'//*[@id="grid-search-results"]/ul/li[{c}]') # variable c refer to next item
    driver.execute_script("arguments[0].scrollIntoView();", element)

Post a Comment for "How To “scroll Down” Some Part Using Selenium In Python?"