19

Today, I learn the lib called pyppeteer,When I run my code

import asyncio
from pyppeteer import launch


async def main():
    browser = await launch(options={'devtools': True, 'headless': False})
    page = await browser.newPage()
    await page.goto('http://example.com')
    await page.screenshot({'path': 'baidu.png'})
    await browser.close()


asyncio.get_event_loop().run_until_complete(main())

I got:

pyppeteer.errors.BrowserError: Browser closed unexpectedly:
Sugan Zhang
  • 233
  • 1
  • 2
  • 12

7 Answers7

23

This is because pyppeteer will not install the required dependencies by chromium. So you should install them yourself.

Execute ldd ~/.local/share/pyppeteer/local-chromium/588429/chrome-linux/chrome | grep 'not found' to fetch the lost dependencies:

Example output:

libXcursor.so.1 => not found
libnss3.so => not found
libnssutil3.so => not found
libsmime3.so => not found
libcups.so.2 => not found
libXss.so.1 => not found
libpangocairo-1.0.so.0 => not found
libpango-1.0.so.0 => not found
libcairo.so.2 => not found
libatk-1.0.so.0 => not found
libatk-bridge-2.0.so.0 => not found
libgtk-3.so.0 => not found
libgdk-3.so.0 => not found
libgdk_pixbuf-2.0.so.0 => not found

You can install them one by one, or install google-chrome to install its dependencies.

For ubuntu/debian:

wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | sudo tee /etc/apt/sources.list.d/google-chrome.list
sudo apt update 
sudo apt install google-chrome-stable

Execute ldd ~/.local/share/pyppeteer/local-chromium/588429/chrome-linux/chrome | grep 'not found' again, there may be some dependencies missed:

libXss.so.1 => not found

Then execute apt install libxss1 to install the missed libXss.so.1

Extra

If you want do screenshot, maybe you need some CJK fonts:

sudo apt install fonts-wqy-zenhei

BaiJiFeiLong
  • 3,716
  • 1
  • 30
  • 28
16

For me, I was running in docker and it ended up being that chromium didn't install the needed libs properly: https://techoverflow.net/2018/06/05/how-to-fix-puppetteer-error-while-loading-shared-libraries-libx11-xcb-so-1-cannot-open-shared-object-file-no-such-file-or-directory/

Gretski
  • 461
  • 4
  • 7
13

I got same and when I tried to start chromium from terminal I get notice that it needs to be run with no sandbox arg, so just add it and you code will work:

browser=await launch(options={'args': ['--no-sandbox']})
Vladmir
  • 545
  • 1
  • 4
  • 8
2

I think we need to install the drivers of chrome .

sudo apt-get install chromium-chromedriver

Thats the issue i am geeting

2

I had the same problem running pyppeteer via cron.

I had previously added

export http_proxy=...

to my cron env since without that, pyppeteer would time out getting documents. With http_proxy, pyppeteer would close unexpectedly.

Comparing my env (where it worked) with that of a cron job, I found what the cron job was missing was

export no_proxy=localhost,127.0.0.1
bers
  • 4,817
  • 2
  • 40
  • 59
  • 1
    Exactly this in my case. The only difference is I had proxy connection set up from inside the script as `os.environ['http_proxy']` – kK-Storm Apr 18 '23 at 11:09
1

To know exactly the reason you can run these two commands on your python3 cmd:

from pyppeteer.launcher import Launcher
' '.join(Launcher().cmd)

and view the results.

But mostly the reason is that you are running your python script as root. You either need to add the "--no-sandbox" configuration or just run the script as another user (any user rather than root)

Yashar
  • 31
  • 4
1

None of the other answers worked for me, as the repos kept failing to locate the packages needed.

However, installing the all of the necessary dependencies can be achieved with the following.

sudo apt install -y gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget

for reference: https://techoverflow.net/2020/09/29/how-to-fix-pyppeteer-pyppeteer-errors-browsererror-browser-closed-unexpectedly/

Kevin Glynn
  • 1,364
  • 1
  • 11
  • 9