如何在Python Selenium中触发谷歌地图评论窗口mouse scroll event
Great question! Google Maps' review section uses custom event handling that often blocks basic scroll attempts like window.scrollTo(). The core issue is you're targeting the wrong element—reviews live inside a specific scrollable container, not the main page body. Here are actionable, proven workarounds:
1. Directly Manipulate the Review Container's Scroll Position
This is the most reliable approach, as it bypasses event interception by directly modifying the container's scroll properties.
Step-by-Step Code:
First, locate the scrollable review container (use your browser's dev tools to confirm the selector—this one works for most Google Maps pages):
from selenium import webdriver from selenium.webdriver.common.by import By import time driver = webdriver.Chrome() driver.get("https://www.google.com.tw/maps/place/%E8%87%AA%E7%94%B1%E5%A5%B3%E7%A5%9E%E5%83%8F/@40.6892494,-74.0466891,17z/data=!3m1!4b1!4m7!3m6!1s0x89c25090129c363d:0x40c6a5770d25022b!8m2!3d40.6892494!4d-74.0445004!9m1!1b1") # Wait for the review container to load (adjust wait time as needed) time.sleep(3) comment_container = driver.find_element(By.CSS_SELECTOR, "div.m6QErb.DxyBCb.kA9KIf.dS8AEf")
Then scroll to the bottom of the container:
# Scroll to the bottom in one go driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight;", comment_container)
For lazy-loaded reviews (most real-world cases), loop to load all content:
while True: current_scroll_height = driver.execute_script("return arguments[0].scrollHeight;", comment_container) # Scroll to bottom driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight;", comment_container) # Wait for new reviews to load time.sleep(2) new_scroll_height = driver.execute_script("return arguments[0].scrollHeight;", comment_container) # Exit loop if no new content is loaded if new_scroll_height == current_scroll_height: break
2. Simulate a Real Mouse Wheel Event
If direct scroll manipulation doesn't trigger lazy loading (some pages rely on wheel events to load content), simulate a native wheel event on the container:
# First, ensure the container is focused (critical for event recognition) comment_container.click() # Dispatch a wheel event with large deltaY to mimic scrolling down driver.execute_script(""" const container = arguments[0]; const wheelEvent = new WheelEvent('wheel', { deltaY: 1000, // Adjust this value for scroll speed bubbles: true, cancelable: true, view: window }); container.dispatchEvent(wheelEvent); """, comment_container)
Wrap this in a loop (like the first method) to load all reviews incrementally.
3. Pre-Focus the Container (Don't Skip This!)
Google Maps often ignores scroll events if the review container isn't in focus. Always ensure it's visible and active first:
# Scroll the container into view if it's off-screen driver.execute_script("arguments[0].scrollIntoView({behavior: 'smooth', block: 'center'});", comment_container) # Click to focus the container comment_container.click()
Key Notes:
- Selector Updates: Google occasionally changes their class names. Use your browser's dev tools to inspect the review container and update the CSS selector if needed.
- Robust Waits: Replace
time.sleep()with explicit waits (e.g.,WebDriverWait) for production code—this avoids race conditions when elements load asynchronously.
内容的提问来源于stack exchange,提问作者Dean

