fix(playwright): 确保浏览器 headless 模式参数为布尔类型

- 在启动浏览器时,检查 headless 参数的类型
- 如果 headless 参数为字符串类型,将其转换为布尔类型
- 支持 "true", "1", "yes", "on" 等字符串作为 True 值
- 其他非布尔类型的 headless 参数将被转换为 True
This commit is contained in:
danial
2025-09-18 02:04:32 +08:00
parent 99d9ee503a
commit a348f74c63

View File

@@ -280,36 +280,21 @@ class DistributedPlaywrightManager:
# 如果上下文已存在,先关闭
if context_id in cls._contexts:
logger.info("上下文已存在,先关闭")
await cls.close_context(context_id)
try:
# Apple网站优化的上下文配置
default_context_options: dict[str, Any] = {
"viewport": {"width": 1366, "height": 768},
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"locale": "en-US",
"timezone_id": "America/New_York",
"geolocation": {"longitude": -74.006, "latitude": 40.7128},
"permissions": ["geolocation"],
"extra_http_headers": {
"Accept-Language": "en-US,en;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
"Cache-Control": "no-cache",
"Pragma": "no-cache",
},
}
# 合并自定义选项
default_context_options.update(context_options)
if not cls._browser:
raise RuntimeError("Browser实例不可用")
context = await cls._browser.new_context(**default_context_options)
logger.info("创建新上下文")
context = await cls._browser.new_context()
logger.info("设置默认超时")
# 设置默认超时
context.set_default_timeout(60000)
context.set_default_navigation_timeout(30000)
logger.info("应用上下文选项")
cls._contexts[context_id] = context