在 WSL Ubuntu 上以 --headless --no 沙箱的形式运行“google-chrome”会出现多个错误 - 如何在 WSL 中使用 headless 截取屏幕截图?

Ang*_*elo 8 ubuntu google-chrome windows-subsystem-for-linux google-chrome-headless

我正在运行 Windows 10,并且一直在 Windows Linux 子系统下使用 Ubuntu Linux 发行版。

我希望能够使用 Chrome.exe 或 google-chrome 无头运行并截取屏幕截图、生成 pdf 等...作为库调用的一部分,但看不到这样做(例如,使用 jupyter_to_medium 库在截屏的背景)。

我已经尝试了多种方法来独立于我正在使用的库或程序使用 google-chrome 调试它。

这是我尝试过的:

  1. 可以通过 Ubuntu 终端启动 Chrome(Windows 可执行文件),并将其重定向到 Windows 可执行文件:

    /mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe
    
    Run Code Online (Sandbox Code Playgroud)

    当我这样做时,我的 Windows Chrome 浏览器中会打开一个新选项卡,所以这里没有问题。

  2. 当我尝试google-chrome在 ubuntu 终端中使用headless 时,我遇到了多个问题,例如

    2a) 尝试获取屏幕截图 -失败显示Network service crashed, restarting service无限循环 - 必须按 CTRL^C 来停止它

     (base) etzimopoulos@DESKTOP-HI9P6E4:~$ google-chrome --headless --disable-gpu --screenshot https://www.chromestatus.com/
    [0626/055458.471044:ERROR:udev_watcher.cc(61)] Failed to enable receiving udev events.
    [0626/055458.612113:ERROR:network_service_instance_impl.cc(262)] Network service crashed, restarting service.
    [0626/055458.696014:ERROR:network_service_instance_impl.cc(262)] Network service crashed, restarting service.
    ...
    
    Run Code Online (Sandbox Code Playgroud)

    2b) 尝试使用不同的参数 --no-sandbox --disable-setuid-sandbox获取屏幕截图,这次得到不同的错误。

    $ sudo google-chrome --no-sandbox --disable-setuid-sandbox --headless --screenshot https://www.chromestatus.com/
    [0626/061854.348849:ERROR:udev_watcher.cc(61)] Failed to enable receiving udev events.
    [0626/061855.311675:ERROR:platform_shared_memory_region_posix.cc(46)] Descriptor access mode (0) differs from expected (2)
    [0626/061855.312483:WARNING:crash_handler_host_linux.cc(366)] Could not translate tid - assuming crashing thread is thread group leader; syscall_supported=0
    [0626/061855.474617:WARNING:crash_handler_host_linux.cc(366)] Could not translate tid - assuming crashing thread is thread group leader; syscall_supported=0
    --2020-06-26 06:18:55--  https://clients2.google.com/cr/report
    Resolving clients2.google.com (clients2.google.com)... 216.58.210.46, 2a00:1450:4009:800::200e
    Connecting to clients2.google.com (clients2.google.com)|216.58.210.46|:443... connected.
    HTTP request sent, awaiting response... [0626/061855.655013:ERROR:headless_shell.cc(399)] Abnormal renderer termination.
    429 Too Many Requests
    2020-06-26 06:18:55 ERROR 429: Too Many Requests.
    
    
    Unexpected crash report id length
    Failed to get crash dump id.
    Report Id: ```
    
    
    Run Code Online (Sandbox Code Playgroud)
  3. 尝试使用相同的参数运行Windows 可执行文件 Chrome.exe,似乎运行但未截取屏幕截图(我的目录中没有新文件)

  4. 尝试在 WSL 中google-chrome 没有提升权限的情况下运行我得到一个不同的错误:

    google-chrome --headless --disable-gpu --screenshot https://www.chromestatus.com/ Failed to move to new namespace: PID namespaces supported, Network namespace supported, but failed: errno = Permission denied Failed to generate minidump.Illegal instruction (core dumped)

任何帮助或指导将不胜感激。我的另一个选择是在带有本机 Linux UI 和终端的 Windows 10 上运行 VM,这可能会奏效。

但是有没有办法在 WSL 中进行所有设置?

谢谢你。

Jin*_*mcg 5

试试这个(在 wsl2 中测试)

在您的 wsl2 ubuntu 控制台中输入以下命令:

#install packages
sudo apt-get install -y curl unzip xvfb libxi6 libgconf-2-4
#get latest chrome
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

#install it
sudo apt install ./google-chrome-stable_current_amd64.deb

#test a screenshot
google-chrome --headless --disable-gpu --screenshot https://www.chromestatus.com/
Run Code Online (Sandbox Code Playgroud)

  • 效果很好! (2认同)