Scrolling web page in selenium, python

When working with selenium, i need to simulate the scrolling webpage action. So after a while, I learnt from many one on Stackoverflow and WordPress blog, here they are my code to the the job. The two functions work similar but

scrollDown

use to avoid unlimit web page length (like facebook: the more you scroll down, the longer it is)

def scrolling_in_website(self, sleep_time, numberOfScrollDowns = 5, disable_scroll = False):
        '''
        -- sleep_time: resting time between scrollings
        simulate mouse scrolling in a webpage
        only scroll when that is a webpage
        '''
        if True: # to make it for debugging
            sleep_time = int(sleep_time%30) + 5
        if disable_scroll is True:
            print('no scrolling at all')
            return  None
        browser = self.driver
        if self.driver.current_url.startswith('http'):
            restime = sleep_time/5
            body = browser.find_element_by_tag_name("body")
            while numberOfScrollDowns >=0:
                body.send_keys(Keys.PAGE_DOWN)
                numberOfScrollDowns -= 1
                time.sleep(restime)
            #heigh = self.driver.execute_script('return document.body.scrollHeight')
            #for i in range(5):
            #    self.driver.execute_script("window.scrollBy(0,document.body.scrollHeight/5)")
            #    time.sleep(restime)
        return
    def scrollDown(self,numberOfScrollDowns = 5):
        browser = self.driver
        if self.driver.current_url.startswith('http'):
            body = browser.find_element_by_tag_name("body")
            while numberOfScrollDowns >=0:
                body.send_keys(Keys.PAGE_DOWN)
                numberOfScrollDowns -= 1
                time.sleep(0.5)
        return browser
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s