Anshul K
Anshul K

Reputation: 1

Scroll down google maps webpage using selenium python

I am trying to collect data on a google map webpage, however i can't find a way to scroll down the page to get all the data.

I'm using python 3 and Selenium package,

thank you in advance,

Upvotes: 0

Views: 1769

Answers (3)

AH.
AH.

Reputation: 41

Try this code it works for me

SCROLL_PAUSE_TIME = 5

last_height = driver.execute_script("return document.body.scrollHeight")
    
number = 0
    
while True:
    number = number+1

Scroll down to bottom

ele = driver.find_element_by_xpath('//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[2]')
driver.execute_script('arguments[0].scrollBy(0, 5000);', ele)

Wait to load page

time.sleep(SCROLL_PAUSE_TIME)

Calculate new scroll height and compare with last scroll height

ele = driver.find_element_by_xpath('//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[2]')

new_height = driver.execute_script("return arguments[0].scrollHeight", ele)

if new_height == last_height:
     break

 last_height = new_height

Upvotes: 1

Dana
Dana

Reputation: 1

To scroll down with selenium:

driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')

and smooth way

driver.execute_script('window.scrollTo({ top: document.body.scrollHeight, behavior: "smooth" });')

Upvotes: 0

pmadhu
pmadhu

Reputation: 3433

You can try to locate the element and use browserelement.execute_script("arguments[0].scrollIntoView(true);",option)

For example:

driver.get("https://www.google.com/maps/place/Bengaluru,+Karnataka/@12.95396,77.4908522,11z/data=!4m5!3m4!1s0x3bae1670c9b44e6d:0xf8dfc3e8517e4fe0!8m2!3d12.9715987!4d77.5945627")
option = driver.find_element_by_xpath("//div[@class='hNLpDc-HiaYvf-DWDkFd-HiaYvf-haAclf']")
driver.execute_script("arguments[0].scrollIntoView(true);",option)

Upvotes: 0

Related Questions