'pyppeteer.errors.BrowserError: Browser closed unexpectedly

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:


Solution 1:[1]

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/

Solution 2:[2]

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']})

Solution 3:[3]

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

Solution 4:[4]

I think we need to install the drivers of chrome .

sudo apt-get install chromium-chromedriver

Thats the issue i am geeting

Solution 5:[5]

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)

Solution 6:[6]

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 the 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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gretski
Solution 2 Vladmir
Solution 3
Solution 4 Navin Dalal
Solution 5 Yashar
Solution 6 bers