聊聊Puppeteer launch参数与Chromium启动参数
Author:zhoulujun Date:
LaunchOptions
puppeteer.launch 参数还蛮多,官网 https://pptr.dev/api/puppeteer.launchoptions
参数很多,其是要记的,也没有几个,需要留意的是:
headless:如过需要查看浏览器,就要设置为false
executablePath:指定 Chromium 或 Chrome 的可执行文件路径,因为Puppeteer自带浏览器无法安装成功。linux上面,大多需要手动指定
slowMo:延迟 Puppeteer 的每个操作,单位为毫秒,用于调试。
defaultViewport:做截图或者PDF的时候,需要制定尺寸——默认值: { width: 800, height: 600 }
dumpio:是否将浏览器进程的 stderr 和 stdout 输出到终端,便于调试。
ignoreHTTPSErrors:忽略 HTTPS 安全错误(如自签名证书问题)。
protocol:制定调式协议,比如把默认值'cdp' 改为'webDriverBiDi'
timeout: 浏览器启动的超时时间(单位为毫秒)。如果设置为 0,则不限制超时时间。
args:传递给 Chromium 的自定义启动参数。
下面是完整的参数表格
Property | Modifiers | Type | Description | Default |
---|---|---|---|---|
args |
| string[] | Additional command line arguments to pass to the browser instance. | |
browser |
| Which browser to launch. |
| |
channel |
| If specified for Chrome, looks for a regular Chrome installation at a known system location instead of using the bundled Chrome binary. | ||
debuggingPort |
| number | Specify the debugging port number to use | |
devtools |
| boolean | Whether to auto-open a DevTools panel for each tab. If this is set to |
|
dumpio |
| boolean | If true, pipes the browser process stdout and stderr to |
|
env |
| Record<string, string | undefined> | Specify environment variables that will be visible to the browser. | The contents of |
executablePath |
| string | Path to a browser executable to use instead of the bundled browser. Note that Puppeteer is only guaranteed to work with the bundled browser, so use this setting at your own risk. Remarks: When using this is recommended to set the | |
extraPrefsFirefox |
| Record<string, unknown> | Additional preferences that can be passed when launching with Firefox. | |
handleSIGHUP |
| boolean | Close the browser process on |
|
handleSIGINT |
| boolean | Close the browser process on |
|
handleSIGTERM |
| boolean | Close the browser process on |
|
headless |
| boolean | 'shell' | Whether to run the browser in headless mode. Remarks:
|
|
ignoreDefaultArgs |
| boolean | string[] | If |
|
pipe |
| boolean | Connect to a browser over a pipe instead of a WebSocket. Only supported with Chrome. |
|
timeout |
| number | Maximum time in milliseconds to wait for the browser to start. Pass |
|
userDataDir |
| string | Path to a user data directory. see the Chromium docs for more info. | |
waitForInitialPage |
| boolean | Whether to wait for the initial page to be ready. Useful when a user explicitly disables that (e.g. |
|
Chromium启动参数
google文档:https://code.google.com/archive/p/xiaody/wikis/ChromiumCommandLineSwitches.wiki
https://peter.sh/experiments/chromium-command-line-switches/
看表格,简直要哭晕。
这里放些我觉得常用的
参数 | 描述 | 注意事项 |
---|---|---|
浏览器行为 | ||
--no-sandbox | 禁用沙盒模式。 | 降低安全性,仅在可信环境使用。 |
--disable-setuid-sandbox | 禁用 setuid 沙盒,通常与 --no-sandbox 一起使用。 | 配合 --no-sandbox 使用效果更佳(仅限Linux)。 |
--start-maximized | 启动时窗口最大化。 | 与 --window-size 不冲突,可结合使用。 |
--disable-infobars | 禁用 "Chrome 正由自动化测试软件控制" 的提示信息。 | 无安全性影响。 |
--incognito | 以无痕模式启动浏览器。 | 会清空所有历史记录和缓存。 |
--disable-popup-blocking | 禁用弹窗拦截功能。 | 用于自动化测试需要触发弹窗的场景。 |
--disable-dev-shm-usage | 禁用 /dev/shm ,避免共享内存不足的问题(在 Docker 中常用)。 | 对性能可能有轻微影响。 |
性能优化 | ||
--disable-extensions | 禁用所有扩展程序。 | 可提高启动速度,减少资源占用。 |
--disable-background-networking | 禁止浏览器进行后台网络请求。 | 可降低不必要的流量消耗。 |
--enable-features=NetworkService,NetworkServiceInProcess | 启用 Chromium 的网络服务优化。 | 对网络请求优化效果明显。 |
--disable-renderer-backgrounding | 防止页面在后台运行时限制资源使用。 | 可用于确保测试场景的前台性能。 |
--disable-features=IsolateOrigins,site-per-process | 禁用站点隔离功能,提高性能。 | 降低安全性,需根据需求权衡使用。 |
调试与开发 | ||
--remote-debugging-port=9222 | 启用远程调试,指定调试端口。 | 开发时调试浏览器行为很有用。 |
--auto-open-devtools-for-tabs | 启动时自动为所有标签页打开开发者工具。 | 适合调试测试脚本时使用。 |
--enable-logging | 启用详细日志输出。 | 配合 --v=1 使用,便于调试。 |
--user-data-dir | 指定用户目录,做用户数据隔离 | 开启多个浏览器,测试不同用户 |
--disable-web-security | 禁止安全,比如CSP 做cros跨域 | |
--default-background-color | 指定背景颜色 | |
显示与分辨率 | ||
--window-size=1920,1080 | 指定窗口大小(宽度,高度)。 | 与 defaultViewport: null 配合,避免 Puppeteer 覆盖设置。 |
--force-device-scale-factor=1 | 强制设置设备的缩放因子。 | 适用于高 DPI 显示器的优化。 |
--disable-gpu | 禁用 GPU 硬件加速。 | 在无 GPU 的服务器环境中常用。 |
--hide-scrollbars | 隐藏滚动条。 | 用于生成截图时保持页面整洁。 |
网络与代理 | ||
--proxy-server=host:port | 设置代理服务器。 | 需指定合法的代理格式。 |
--proxy-bypass-list=hosts | 指定不走代理的主机列表。 | 可结合 --proxy-server 一起使用。 |
--ignore-certificate-errors | 忽略 SSL/TLS 证书错误。 | 对测试自签名证书的网站很有用,但降低安全性。 |
完整的参数表格就不放了
下面是我的配置
'--no-sandbox', # 不使用沙箱
'--proxy-server=http://127.0.0.1:1080', # 代理
'--no-startup-window', # 禁止开始界面
'--incognito', # 隐身模式
'--disable-infobars', # 禁止信息提示栏
'--disable-gpu', # 不用gpu
'--disable-setuid-sandbox', # 与--no-sandbox配合
'--no-default-browser-check', # 不检查默认浏览器
'--disable-extensions', # 禁止扩展
'--disable-default-apps', # 禁止默认应用
'--disable-dev-shm-usage', # 禁止使用/dev/shm,防止内存不够用
'--disable-hang-monitor', # 禁止页面无响应提示
'--disable-popup-blocking', # 禁止popup
'--disable-prompt-on-repost', # 禁止重新发送post请求的提示
'--disable-sync', # 禁止同步
'--disable-translate', # 禁止翻译
'--disable-bundled-ppapi-flash', # 禁止内置的flash
'--disable-component-update', # 禁止组件升级
'--disable-background-networking', # 禁止后台的网络连接
'--disable-background-timer-throttling', # 禁止后台任务冻结
'--disable-client-side-phishing-detection', # 禁止危险页面检测
'--disable-logging',
'--mute-audio', # 静音
'--single-process', # 单进程
'--no-zygote', # 禁止zygote进程fork子进程
'--safebrowsing-disable-auto-update',
'--no-first-run', # 禁止首次运行界面
'--hide-scrollbars', # 隐藏滚动栏
'--use-mock-keychain', # 使用mock-keychain防止提示权限提示
'--ignore-certificate-errors', # 忽略证书错误
转载本站文章《聊聊Puppeteer launch参数与Chromium启动参数》,
请注明出处:https://www.zhoulujun.cn/html/webfront/browser/Puppeteer/9442.html