Search code examples
pythonhtmlselenium-webdriver

Scrolling to the bottom of a div element using Selenium (Python)


I want to scroll a particular div element in a website until it reaches its bottom.
Say i have this html:

<div>
    <ul>
        <li>list1</li>
        <li>list2</li>
        <li>list3</li>
    </ul>
    <ol>
        <li>previous</li>
        <li>current</li>
        <li>next</li>
    </ol>

</div>

Now the <ol> element is out of my viewport at the bottom of the page and in the entire page, the div listed above is the only scrollable element. How do I scroll to the bottom of this so I can have <ol> in my viewport and i could interact with it using selenium? Like using .click(). I cannot juts scroll to the bottom of the page as only the div is scrollable and trying to scroll anywhere else does nothing.

Looking at previous StackOverflow threads I tried to locate the div and then scroll along it using javascript like so:

div = self.driver.find_element(By.XPATH, "/html/body/main/section/section/div[2]/div")
self.driver.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', div)

I hoped this was would scroll the div to the <ol> list and i could interact with it. Unfortunately this did not do anything and i still got a NoSuchElementException error.


Solution

  • Looking at the HTML code of the page provided in the comments, the <ol> tag is enclosed in the <nav> tags with id of paging. You can use the #paging CSS selector to get to these elements.

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    
    driver = webdriver.Firefox()
    driver.get("https://www.homes.com/san-francisco-ca/homes-for-rent/1-to-5-bedroom/?price-max=3000")
    driver.implicitly_wait(5)
    nav_tag = driver.find_element(By.CSS_SELECTOR, "#paging")
    driver.execute_script("arguments[0].scrollIntoView();", nav_tag)
    driver.implicitly_wait(5)
    driver.quit()