pythonselenium

How to scroll down google maps using selenium python


I am trying to scroll down the google maps page using selenium but unable to do so. I have tried all the things written here: Scroll down google maps webpage using selenium python

Here is my code:

def scroll(self):        
    SCROLL_PAUSE_TIME = 5

    # Get scroll height
    last_height = self.execute_script("return document.body.scrollHeight") 

    number = 0

    while True:
        number = number+1

        # Scroll down to bottom
        
        ele = self.find_element_by_css_selector('div[jstcache="211"]')
        print(ele)
        self.execute_script('arguments[0].scrollBy(0, 500)', ele)

        # Wait to load page

        time.sleep(SCROLL_PAUSE_TIME)

        # Calculate new scroll height and compare with last scroll height
        print(f'last height: {last_height}')

        ele = self.find_element_by_css_selector('div[jstcache="211"]')
        
        new_height = self.execute_script("return arguments[0].scrollHeight", ele) 

        print(f'new height: {new_height}')

        if number == 5:
            break

        if new_height == last_height:
            break

        print('cont')
        last_height = new_height

but cannot figure out how to scroll down.

Kindly help!!


Solution

  • The solution which worked for me is:

    def scroll(self):
        for i in range(6,25,3): 
            last_review = self.find_elements_by_css_selector('div[jstcache="192"]')
            self.execute_script('arguments[0].scrollIntoView(true);', last_review[i])
            time.sleep(5)
    

    The idea behind this is that every time fetch all the business boxes and scroll into view the last one , then wait , fetch again, scroll into view the last again and so on.