Reputation: 119
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!!
Upvotes: 0
Views: 4862
Reputation: 661
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.
Upvotes: 6
Reputation: 1
To scroll to the end of the result search list in Google Maps, you have to change the browser language first to English. Then deactivate side bar and past this code:
# scroll down in google maps with python selenium javascript
while True:
results = driver.find_elements(By.CLASS_NAME, 'hfpxzc')
driver.execute_script("return arguments[0].scrollIntoView();", results[-1])
page_text = driver.find_element(by=By.TAG_NAME, value='body').text
endliststring="You've reached the end of the list."
if endliststring not in page_text:
driver.execute_script("return arguments[0].scrollIntoView();", results[-1])
else:
break
driver.execute_script("return arguments[0].scrollIntoView();", results[-1])
Upvotes: 0
Reputation: 11
My issue was resolved by replacing the class name with your location:
results = driver.find_elements(By.CLASS_NAME, 'hfpxzc')
driver.execute_script("return arguments[0].scrollIntoView();", results[-1])
Upvotes: 1
Reputation: 31
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)
Upvotes: 0
Reputation: 119
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.
Upvotes: 1
Reputation: 41
I used the same code and you can fix it by deleting the following lines :)
if number == 5:
break
Upvotes: 0
Reputation: 30
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
Upvotes: 0