使用Selenium批量搜索谷歌地图时遇chrome not reachable错误求助
解决Chrome Not Reachable错误的方案
1. 匹配ChromeDriver与Chrome版本
你的Chrome版本为97.0.4692.71,必须下载完全对应版本的ChromeDriver,替换现有chromedriver.exe后,确保路径指向正确文件。
2. 修正代码中的关键问题
核心错误修正
- 旧版
chrome_options参数已废弃,改用options参数初始化驱动 - 统一用户目录路径(原代码中chromedriver路径指向
admin用户,Chrome二进制路径指向sonpn.vbi用户,存在路径不一致问题) - 初始化
convert空列表(原代码未定义该变量)
优化后的完整代码
from selenium import webdriver import time list_x = ['A', 'B', 'C', 'D'] convert = [] # 初始化存储结果的列表 # 修正路径:确保与当前用户目录一致 chromedriver_path = r"C:\Users\sonpn.vbi\chromium-browser\chromedriver.exe" options = webdriver.ChromeOptions() options.binary_location = r"C:\Users\sonpn.vbi\AppData\Local\Google\Chrome\Application\chrome.exe" # 添加可选启动参数,避免环境异常 options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") options.add_argument("--start-maximized") # 正确初始化Chrome驱动 driver = webdriver.Chrome(executable_path=chromedriver_path, options=options) driver.get("https://www.google.co.in/maps/@10.8091781,78.2885026,7z") time.sleep(2) for item in list_x: # 清空搜索框再输入,避免叠加内容 search_box = driver.find_element("class name", "tactile-searchbox-input") search_box.clear() search_box.send_keys(item) # 点击搜索按钮 driver.find_element("xpath", "//*[@id='searchbox-searchbutton']").click() time.sleep(3) # 改用更稳定的CSS选择器定位结果,避免绝对xpath失效 result_elements = driver.find_elements("css selector", "[data-testid='place-name']") convert.extend([el.text for el in result_elements if el.text]) # 修正关闭按钮定位 close_btn = driver.find_element("css selector", ".searchbox-closebutton") close_btn.click() time.sleep(1) # 最后关闭浏览器进程 driver.quit()
3. 额外排查步骤
- 关闭所有Chrome进程后再运行脚本,避免残留进程占用端口
- 以管理员身份运行Python脚本,解决权限限制问题
内容的提问来源于stack exchange,提问作者Son Pham
相关产品推荐
相关产品推荐

