简体   繁体   中英

How to scroll the results shown by google maps search using selenium

I want to get all the 20 results given but currently, I am unable to get that. This is my code and I am unable to scroll to the bottom to get all the results. I can only get the first 7 records returned. Please guide what else steps are needed to achieve this result.

driver = webdriver.Chrome(PATH, options=options)
driver.set_page_load_timeout(20)
driver.implicitly_wait(10)
driver.get(base_url)
driver.maximize_window()
web_element = driver.find_element_by_id("searchboxinput")
web_element.send_keys("hospital in delhi " + "\n")
web_element.submit()
time.sleep(10)
outer_grid = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, 'mapsConsumerUiCommonScrollable__scrollable-show')))
print("total grid size result is :{}".format(len(outer_grid)))
if len(outer_grid) == 0:
    print("Empty result try again")
    exit(-1)

time.sleep(3)

result_table = outer_grid[0]
driver.execute_script('arguments[0].scrollTo(0, document.body.scrollHeight);', result_table)
h_ele = WebDriverWait(result_table, 10).until(
    EC.visibility_of_all_elements_located((By.CLASS_NAME, "sJKr7qpXOXd__result-container")))
print("Total results from lot : {}".format(len(h_ele)))

for ele in h_ele:
    print("Name : {}".format(ele.text))
    website = ele.find_element_by_tag_name("a")
    http_url = "NA"
    if website.get_attribute("href") is not None:
        http_url = website.get_attribute("href")
        print("website : {}".format(http_url))

driver.quit()

Some websites do not trigger the automatic load so easily. If you need to do this more than once then you'll have to implement a loop. I have two ways of dealing with this:

"Smooth Scrolling"

total_height = int(driver.execute_script("return document.body.scrollHeight"))
for i in range(1, total_height, 4):
   driver.execute_script("window.scrollTo(0, {});".format(i))

"Pseudo Smooth Scrolling"

h_ele = WebDriverWait(result_table, 10).until(
    EC.visibility_of_all_elements_located((By.CLASS_NAME, "sJKr7qpXOXd__result-container")))
loc = h_ele[-1].location['y']
driver.execute_script(f"window.scrollTo(0, {loc} + 200);")
driver.execute_script(f"window.scrollTo(0, {loc} + 210);")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM