chrome-headless

由于内容为空,无法获取关键信息生成摘要。

在这里插入图片描述

你提到了 `chrome-headless-shell-win64`,这是一个与 **Headless Chrome 浏览器**相关的可执行文件,下面我们详细解释它的作用、用途以及如何在 Python + Selenium 中正确使用它。 --- ### ✅ 什么是 `chrome-headless-shell`? `chrome-headless-shell` 是 Google 提供的一个**精简版的 Chrome 浏览器**,专门用于运行在“无头模式”(headless)下,即不显示图形界面,适合服务器或自动化测试环境。 - 它不是驱动(如 chromedriver),而是**浏览器本体的一种特殊构建版本**。 -Chrome 112 开始,Google 推出这个独立包来支持云环境和容器化部署。 - 支持平台:Windows (`win64`)、Linux (`linux64`)、macOS (`mac-arm64`, `mac-x64`) - 典型文件名: ``` chrome-headless-shell-win64.zip ``` > 🔗 下载地址(官方): > [https://googlechromelabs.github.io/chrome-for-testing/#stable](https://googlechromelabs.github.io/chrome-for-testing/#stable) --- ### ❓为什么需要 `chrome-headless-shell`? 传统方式中,Selenium 使用本地安装的完整 Chrome 浏览器 + ChromeDriver 来控制页面。但在以下场景会遇到问题: | 场景 | 问题 | |------|------| | Docker 容器 | 无法安装完整 GUI 浏览器 | | CI/CD 环境(GitHub Actions) | 没有图形界面 | | 服务器部署 | 缺少依赖库(如 X11) | 而 `chrome-headless-shell` 正是为了解决这些问题设计的——它体积小、无需 GUI、专为自动化服务。 --- ## ✅ 如何下载并使用 `chrome-headless-shell-win64` ### ✅ 方法一:手动下载 + 配置 #### 1. 下载地址示例(请根据版本替换): ``` https://storage.googleapis.com/chrome-for-testing-public/143.0.3650.75/win64/chrome-headless-shell-win64.zip ``` > 💡 替换规则: > - 版本号:`143.0.3650.75` > - 平台:`win64` #### 2. 解压后得到: ``` chrome-headless-shell-win64/ └── chrome-headless-shell.exe ``` #### 3. 在 Python 中指定路径启动 ```python from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options import os # 设置路径 chrome_binary_path = os.path.join(os.getcwd(), "chrome-headless-shell-win64", "chrome-headless-shell.exe") driver_path = os.path.join(os.getcwd(), "chromedriver", "chromedriver.exe") # 对应版本的驱动 # 检查文件是否存在 if not os.path.exists(chrome_binary_path): raise FileNotFoundError(f"未找到 chrome-headless-shell: {chrome_binary_path}") if not os.path.exists(driver_path): raise FileNotFoundError(f"未找到 chromedriver: {driver_path}") # 配置选项 options = Options() options.binary_location = chrome_binary_path # 指定使用 headless shell options.add_argument("--headless=new") # 启用新版无头模式 options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") options.add_argument("--disable-gpu") options.add_argument("--remote-debugging-port=9222") # 启动 WebDriver service = Service(executable_path=driver_path) driver = webdriver.Chrome(service=service, options=options) try: driver.get("https://www.baidu.com") print("标题:", driver.title) finally: driver.quit() ``` --- ### ✅ 方法二:自动下载(推荐) 使用 `chromedriver-autoinstaller` 或 `webdriver-manager` 可以自动获取匹配的 `chrome-headless-shell` 和 `chromedriver`。 #### 示例:使用 `webdriver-manager` 自动管理 ```bash pip install webdriver-manager>=4.0.0 ``` ```python from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from webdriver_manager.chrome import ChromeDriverManager from webdriver_manager.core.os_manager import ChromeType # 自动下载 chromedriver(对应 chrome-for-testing) driver_path = ChromeDriverManager( chrome_type=ChromeType.CHROME_HEADLESS_SHELL, cache_path="./drivers" ).install() # 下载或确认 chrome-headless-shell 的路径(需手动处理 binary) # 注意:webdriver_manager 不直接提供 chrome-headless-shell 的二进制,但可以帮你找驱动 chrome_shell_path = "./chrome-headless-shell-win64/chrome-headless-shell.exe" if not os.path.exists(chrome_shell_path): print("请先下载 chrome-headless-shell 并放至指定路径") exit(1) options = Options() options.binary_location = chrome_shell_path options.add_argument("--headless=new") options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") service = Service(executable_path=driver_path) driver = webdriver.Chrome(service=service, options=options) driver.get("https://httpbin.org/ip") print("页面标题:", driver.title) driver.quit() ``` --- ### ✅ 方法三:Docker 中使用(高级用法) Google 提供了官方镜像: ```dockerfile FROM gcr.io/chrome-browser/headless-shell:latest # 安装 Python、Selenium RUN apt-get update && apt-get install -y python3 python3-pip COPY . /app WORKDIR /app RUN pip3 install selenium webdriver-manager CMD ["python3", "test_headless.py"] ``` --- ### ⚠️ 注意事项 | 要点 | 说明 | |------|------| | `--headless=new` | Chrome 112+ 推荐使用此参数启用现代无头模式 | | 必须匹配版本 | `chromedriver` 与 `chrome-headless-shell` 主版本必须一致(如都是 143) | | 不含默认功能 | 如 Flash、某些字体等缺失,注意网页兼容性 | | 日志调试 | 添加 `--log-level=0 --v=1` 查看详细日志 | --- ### ✅ 总结对比 | 工具 | 类型 | 是否必需 | 说明 | |------|------|----------|------| | `chromedriver` | 驱动程序 | ✅ 必需 | 控制浏览器通信 | | `chrome-headless-shell` | 浏览器本体 | ✅(替代普通 Chrome) | 无 GUI 环境专用 | | `chrome`(完整版) | 浏览器本体 | ❌ 可选 | 本地开发可用,不适合服务器 | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值