• home > webfront > browser > Puppeteer >

    聊聊Puppeteer launch参数与Chromium启动参数

    Author:zhoulujun Date:

    puppeteer launch 参数还蛮多,在linux上调试,最需要注意的是 Chromium启动参数,那就更多了,完全证明明白,估计项目都凉凉了

    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

    optional

    string[]

    Additional command line arguments to pass to the browser instance.


    browser

    optional

    SupportedBrowser

    Which browser to launch.

    chrome

    channel

    optional

    ChromeReleaseChannel

    If specified for Chrome, looks for a regular Chrome installation at a known system location instead of using the bundled Chrome binary.


    debuggingPort

    optional

    number

    Specify the debugging port number to use


    devtools

    optional

    boolean

    Whether to auto-open a DevTools panel for each tab. If this is set to true, then headless will be forced to false.

    false

    dumpio

    optional

    boolean

    If true, pipes the browser process stdout and stderr to process.stdout and process.stderr.

    false

    env

    optional

    Record<string, string | undefined>

    Specify environment variables that will be visible to the browser.

    The contents of process.env.

    executablePath

    optional

    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 browser property as well as Puppeteer will default to chrome by default.


    extraPrefsFirefox

    optional

    Record<string, unknown>

    Additional preferences that can be passed when launching with Firefox.


    handleSIGHUP

    optional

    boolean

    Close the browser process on SIGHUP.

    true

    handleSIGINT

    optional

    boolean

    Close the browser process on Ctrl+C.

    true

    handleSIGTERM

    optional

    boolean

    Close the browser process on SIGTERM.

    true

    headless

    optional

    boolean | 'shell'

    Whether to run the browser in headless mode.

    Remarks:

    • true launches the browser in the new headless mode.

    • 'shell' launches shell known as the old headless mode.

    true

    ignoreDefaultArgs

    optional

    boolean | string[]

    If true, do not use puppeteer.defaultArgs() when creating a browser. If an array is provided, these args will be filtered out. Use this with care - you probably want the default arguments Puppeteer uses.

    false

    pipe

    optional

    boolean

    Connect to a browser over a pipe instead of a WebSocket. Only supported with Chrome.

    false

    timeout

    optional

    number

    Maximum time in milliseconds to wait for the browser to start. Pass 0 to disable the timeout.

    30_000 (30 seconds).

    userDataDir

    optional

    string

    Path to a user data directory. see the Chromium docs for more info.


    waitForInitialPage

    optional

    boolean

    Whether to wait for the initial page to be ready. Useful when a user explicitly disables that (e.g. --no-startup-window for Chrome).

    true

    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

    TOP