feat(driver): support BROWSER_TYPE env var to override Chrome auto-detection

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.
This commit is contained in:
longw
2026-07-27 00:15:33 +08:00
parent 6ca61c19a2
commit 072e0ee67e
+12 -4
View File
@@ -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)