You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派Docker容器中Playwright Chromium时区配置问题

解决Docker容器中Playwright Chromium时区不匹配问题

方法1:启动Chromium时指定时区参数

直接在Playwright启动浏览器的代码中,通过args参数传递时区配置,对应CEST的时区可使用Europe/Paris(可根据实际时区调整):

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    # 启动Chromium时指定时区参数
    browser = p.chromium.launch(args=['--timezone=Europe/Paris'])
    page = browser.new_page()
    page.goto("https://example.com")
    page.screenshot(path="screenshot.png")
    browser.close()

如果想动态适配容器时区,可读取容器内的TZ环境变量或/etc/timezone文件内容,再传入参数。

方法2:配置容器环境变量TZ

虽然你已挂载/etc/timezone和/etc/localtime,但Chromium可能优先读取TZ环境变量。启动容器时添加该变量:

docker run -d -e TZ=Europe/Paris -v /etc/timezone:/etc/timezone -v /etc/localtime:/etc/localtime [你的镜像名]

或者在Dockerfile中提前设置:

ENV TZ=Europe/Paris
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

配置后容器内的Chromium会自动识别时区,无需在代码中额外指定参数。

方法3:验证无头模式下的时区传递

若使用无头模式,需确保时区参数正确传递——部分场景下无头模式会忽略系统时区,显式指定--timezone参数更可靠。可先在容器内手动启动Chromium测试:

chromium-browser --headless --timezone=Europe/Paris --screenshot=test.png https://time.is/

确认截图时区正确后,再同步到Playwright代码中。

内容的提问来源于stack exchange,提问作者mu88

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 10:37:36