Bob Ezuba
Bob Ezuba

Reputation: 510

Scroll down html table using selenium and python

I am trying to scroll down an HTML table until the last row is visible, the table initially loads about 20 rows and then loads more as I scroll down. I am using selenium webdriver and python to automate this process but so far i am stuck as my code below is scrolling down the whole page instead of this particular table which is passed as an argument in the function.

here is my code the web element to be scrolled is passed as an argument as well as the number of times it will be scrolled down

def scroll_down_element(self, element, times):
    try:
        action_chain = ActionChains(self.browser)
        counter = 0
        while(counter < times):
            element.send_keys(Keys.ARROW_DOWN)
            counter += 1
            sleep(0.25)
    except Exception as e:
        print 'error scrolling down web element', e

All suggestions are welcome

Upvotes: 3

Views: 12433

Answers (2)

Bob Ezuba
Bob Ezuba

Reputation: 510

This is the code that I resorted to after trying a few different combinations of javascript and other selenium methods. Space scrolls down deeper than ARROW_DOWN or the DOWN Key.

def scroll_down_element(self, element, times):
    try:
        action = ActionChains(self.browser)
        action.move_to_element(element).perform()
        element.click() 
        for _ in range(times):
            element.send_keys(Keys.SPACE)
            sleep(0.1)
    except Exception as e:
        print 'error scrolling down web element', e

Upvotes: 4

Sitam Jana
Sitam Jana

Reputation: 3129

Use JS to scroll within a web element. One example of such is given below:

def scroll_down_element(self, element):
    try:
        self.browser.execute_script("arguments[0].scrollTop = 200", element)

    except Exception as e:
        print 'error scrolling down web element', e

Hope this helps!

Upvotes: 0

Related Questions