Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 422
Bob Ezuba Avatar asked May 10 '26 07:05

Bob Ezuba


1 Answers

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
like image 155
Bob Ezuba Avatar answered May 12 '26 01:05

Bob Ezuba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!