[Python] Selenium + ChromeDriver crash when scraping Google Maps entries with Selenium on MacOS

Discussão em 'Python' iniciado por Stack, Maio 10, 2025.

  1. Stack

    Stack Membro Participativo

    I’m building a Telegram bot in Python that scrapes points of interest (POIs) from Google Maps using Selenium. The overall flow works, but as soon as I try to scroll through and click on the .hfpxzc elements, ChromeDriver crashes with a native stack trace.

    What I’m trying to do is:

    Open Google Maps search results for a list of POI types in a given location. Scroll through the results pane until no new entries load. Click each entry to extract its name, address, website, phone, rating, etc. Filter by geographic radius and return a list of POIs.

    my function inside code where error happening:

    def find_google_maps_pois(location_str, coords, poi_types, radius):
    """Scrape POIs from Google Maps using Selenium and filter by radius."""
    types_str = "+".join(poi_types)
    location_str = location_str.replace(" ", "+")
    url = f"https://www.google.com/maps/search/{types_str}+in+{location_str}"

    print(f"Searching Google Maps for: {url}")

    options = webdriver.ChromeOptions()
    options.add_argument("--headless")
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    browser = webdriver.Chrome(options=options)
    browser.get(url)


    wait = WebDriverWait(browser, 10)
    wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, "hfpxzc")))

    # pfull_html = browser.page_source

    entries = browser.find_elements(By.CLASS_NAME, "hfpxzc")

    SCROLL_PAUSE = 2
    STUCK_LIMIT = 10
    le = 0
    while le < STUCK_LIMIT:
    last = entries[-1]
    browser.execute_script("arguments[0].scrollIntoView(true);", last)
    time.sleep(SCROLL_PAUSE)
    new_entries = browser.find_elements(By.CLASS_NAME, "hfpxzc")
    if len(new_entries) == len(entries):
    le += 1
    else:
    entries = new_entries
    le = 0
    body_el = browser.find_element(By.TAG_NAME, "body")

    body_inner = body_el.get_attribute("innerHTML")

    pois = []
    seen_names = set()
    bt = []


    for entry in entries:
    if True: #I now this is not correct with if True, just pass this
    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h1[class*='fontHeadlineLarge']")))

    data = {}

    # Name
    data['name'] = browser.find_element(By.CSS_SELECTOR, "h1[class*='fontHeadlineLarge']").text.strip()

    print("Name:", data['name'])

    # Address (Google Maps often uses this button selector)
    try:
    data['address'] = browser.find_element(By.CSS_SELECTOR, "button[data-item-id*='address']").text.strip()
    except:
    data['address'] = 'Not found'

    # Website
    try:
    data['website'] = browser.find_element(By.CSS_SELECTOR, "a[data-item-id*='authority']").get_attribute("href")
    except:
    data['website'] = 'Not found'

    # Phone
    try:
    data['phone'] = browser.find_element(By.CSS_SELECTOR, "button[data-item-id*='phone']").text.strip()
    except:
    data['phone'] = 'Not found'

    # Send back to user
    result = "\n".join(f"{k.title()}: {v}" for k, v in data.items())
    print("Result:", result)
    # update.message.reply_text(result)




    name = browser.find_element(By.CSS_SELECTOR, "h1[class*='fontHeadlineLarge']").text.strip()
    print(f"Processing: {name}")
    if name in seen_names:
    continue
    seen_names.add(name)

    try:
    phone = browser.find_element(By.CSS_SELECTOR, "button[aria-label*='Copy phone']").text.strip()
    except NoSuchElementException:
    phone = "N/A"

    try:
    address = browser.find_element(By.CSS_SELECTOR, "button[data-tooltip*='Copy address']").text.strip()
    except NoSuchElementException:
    address = "N/A"

    try:
    website = browser.find_element(By.CSS_SELECTOR, "a[aria-label='Open website']").get_attribute("href")
    except NoSuchElementException:
    website = "N/A"

    try:
    rating_text = browser.find_element(By.CSS_SELECTOR, "span.ceNzKf").text.strip()
    rating, review_count = rating_text.split(" ")
    rating = float(rating)
    review_count = int(review_count[1:-1])
    except (NoSuchElementException, ValueError):
    rating = "N/A"
    review_count = "N/A"

    current_url = browser.current_url
    lat, lon = None, None
    if "@" in current_url:
    parts = current_url.split("@")[1].split(",")
    lat = float(parts[0])
    lon = float(parts[1])

    if lat and lon:
    distance = geodesic(coords, (lat, lon)).meters
    if distance <= radius:
    pois.append({
    "source": "Google Maps",
    "type": ", ".join(poi_types),
    "name": name,
    "address": address,
    "lat": lat,
    "lon": lon,
    "phone": phone,
    "email": "N/A",
    "website": website,
    "rating": rating,
    "review_count": review_count,
    "notes": f"Google Maps URL: {current_url}"
    })
    browser.quit()
    return pois

    Continue reading...

Compartilhe esta Página