How to scroll down google maps using selenium python
Question:
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!!
Answers:
After you enter the site, you will most likely have the side panel open. Close that by clicking the center of the screen once through selenium. Then send arrow keys to the map element.
from selenium.webdriver.common.keys import Keys
driver.find_element(BY.CLASS, "dmCR2e widget-scene").click()
driver.find_element(BY.CLASS, "dmCR2e widget-scene").send_keys(Keys.ARROW_DOWN)
#ARROW_UP ARROW_RIGHT ARROW_LEFT
I used the same code and you can fix it by deleting the following lines 🙂
if number == 5:
break
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.
The other answers didn’t work for me so this is what I came up with.
divSideBar=driver.find_element(By.CSS_SELECTOR,f"div[aria-label='Results for {query}']")
keepScrolling=True
while(keepScrolling):
divSideBar.send_keys(Keys.PAGE_DOWN)
time.sleep(0.5)
divSideBar.send_keys(Keys.PAGE_DOWN)
time.sleep(0.5)
html =driver.find_element(By.TAG_NAME, "html").get_attribute('outerHTML')
if(html.find("You've reached the end of the list.")!=-1):
keepScrolling=False
"query" is just your search query, as you can see in the screenshot here.
This solution worked for me
sidebar_div=driver.find_element(By.XPATH,'//*[@class="TFQHme "]')
sidebar_div.click()
no_scroll=3 # how many times do you want to scroll
for page_down in range(0,no_scroll):
ele = driver.find_element(By.XPATH,'//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[2]/div[1]')
driver.execute_script('arguments[0].scrollBy(0, 5000);', ele)
time.sleep(2)
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!!
After you enter the site, you will most likely have the side panel open. Close that by clicking the center of the screen once through selenium. Then send arrow keys to the map element.
from selenium.webdriver.common.keys import Keys
driver.find_element(BY.CLASS, "dmCR2e widget-scene").click()
driver.find_element(BY.CLASS, "dmCR2e widget-scene").send_keys(Keys.ARROW_DOWN)
#ARROW_UP ARROW_RIGHT ARROW_LEFT
I used the same code and you can fix it by deleting the following lines 🙂
if number == 5:
break
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.
The other answers didn’t work for me so this is what I came up with.
divSideBar=driver.find_element(By.CSS_SELECTOR,f"div[aria-label='Results for {query}']")
keepScrolling=True
while(keepScrolling):
divSideBar.send_keys(Keys.PAGE_DOWN)
time.sleep(0.5)
divSideBar.send_keys(Keys.PAGE_DOWN)
time.sleep(0.5)
html =driver.find_element(By.TAG_NAME, "html").get_attribute('outerHTML')
if(html.find("You've reached the end of the list.")!=-1):
keepScrolling=False
"query" is just your search query, as you can see in the screenshot here.
This solution worked for me
sidebar_div=driver.find_element(By.XPATH,'//*[@class="TFQHme "]')
sidebar_div.click()
no_scroll=3 # how many times do you want to scroll
for page_down in range(0,no_scroll):
ele = driver.find_element(By.XPATH,'//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[2]/div[1]')
driver.execute_script('arguments[0].scrollBy(0, 5000);', ele)
time.sleep(2)