Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyppeteer.errors.BrowserError: Browser closed unexpectedly

Tags:

pyppeteer

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:
like image 412
Sugan Zhang Avatar asked Jul 26 '19 10:07

Sugan Zhang


5 Answers

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/

like image 95
Gretski Avatar answered Oct 16 '22 11:10

Gretski


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']})
like image 20
Vladmir Avatar answered Oct 16 '22 12:10

Vladmir


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

like image 31
BaiJiFeiLong Avatar answered Oct 16 '22 11:10

BaiJiFeiLong


I think we need to install the drivers of chrome .

sudo apt-get install chromium-chromedriver

Thats the issue i am geeting

like image 2
Navin Dalal Avatar answered Oct 16 '22 11:10

Navin Dalal


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)

like image 1
Yashar Avatar answered Oct 16 '22 10:10

Yashar