From 072e0ee67e76b63c0f6e548d43b8be151e1c5f5f Mon Sep 17 00:00:00 2001 From: longw Date: Mon, 27 Jul 2026 00:15:33 +0800 Subject: [PATCH] feat(driver): support BROWSER_TYPE env var to override Chrome auto-detection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When WeChat public platform is reached via a locally detected Chrome binary on Windows, the platform's anti-bot triggers and the QR code fails to load (二维码加载超时). Allow users to force a specific Playwright browser (webkit/chromium/firefox/msedge) via the BROWSER_TYPE environment variable, bypassing the heuristic that overrides the default browser_type with the local Chrome path. Example: BROWSER_TYPE=webkit python main.py This restores cross-platform portability: explicit user preference takes precedence over auto-detection, while the existing default (Playwright webkit when no Chrome is detected) is unchanged. --- driver/playwright_driver.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/driver/playwright_driver.py b/driver/playwright_driver.py index 764eb6ba..1d0bc1d4 100644 --- a/driver/playwright_driver.py +++ b/driver/playwright_driver.py @@ -130,10 +130,18 @@ class PlaywrightController: # 仅当通过环境变量 CHROME_EXECUTABLE_PATH 显式指定(或自动发现到本机标准安装的 # Chromium 内核浏览器)时才启用;否则保持上游默认浏览器(默认 webkit,由 Playwright 自带), # 跨平台可移植,Docker/Linux 默认行为不受影响。 - CHROME_PATH = get_chrome_executable_path() - if CHROME_PATH: - self.browser_type = "chromium" - print_info(f"使用本地 Chrome: {CHROME_PATH}(跳过 Playwright 浏览器下载)") + # 补丁:支持 BROWSER_TYPE 环境变量,用户可强制指定 webkit/chromium/firefox, + # 微信公众平台在 Windows 本地 Chrome 下经常触发反爬(QR 加载超时), + # 此时设置 BROWSER_TYPE=webkit 可绕过。 + _forced_browser = os.environ.get("BROWSER_TYPE", "").strip().lower() + if _forced_browser in ("webkit", "chromium", "firefox", "msedge"): + self.browser_type = _forced_browser + print_info(f"通过 BROWSER_TYPE 强制使用浏览器: {self.browser_type}") + else: + CHROME_PATH = get_chrome_executable_path() + if CHROME_PATH: + self.browser_type = "chromium" + print_info(f"使用本地 Chrome: {CHROME_PATH}(跳过 Playwright 浏览器下载)") # 选择浏览器类型 browser_launcher = getattr(self._playwright, self.browser_type)