3

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

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

thanks this worked for me! If the element is not interactable then you can still send keys using actions.send_keys(Keys.DOWN).perform()
0

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!

1 Comment

unfortunately, the above code was resulting into an error... I will post the answer below

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.