Forums

tips for pyppeteer error? Browser closed unexpectedly

Hi all,

I'm looking to use pyppeteer, but running sample code, I keep getting pyppeteer.errors.BrowserError: Browser closed unexpectedly:

import asyncio
from pyppeteer import launch
async def main():
  # launch chromium browser in the background
  browser = await launch()
  # open a new tab in the browser
  page = await browser.newPage()
  # add URL to a new page and then open it
  await page.goto("https://www.python.org/")
  # create a screenshot of the page and save it
  await page.screenshot({"path": "/home/greencracker/sandbox/python.png"})
  # close the browser
  await browser.close()

print("Starting...")
asyncio.get_event_loop().run_until_complete(main())
print("Screenshot has been taken")

result is

Traceback (most recent call last):
  File "/home/greencracker/sandbox/sandbox.py", line 22, in <module>
    asyncio.get_event_loop().run_until_complete(main())
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/home/greencracker/sandbox/sandbox.py", line 11, in main
    browser = await launch()
  File "/home/greencracker/.virtualenvs/puppeteer/lib/python3.9/site-packages/pyppeteer/launcher.py", line 307, in launch
    return await Launcher(options, **kwargs).launch()
  File "/home/greencracker/.virtualenvs/puppeteer/lib/python3.9/site-packages/pyppeteer/launcher.py", line 168, in launch
    self.browserWSEndpoint = get_ws_endpoint(self.url)
  File "/home/greencracker/.virtualenvs/puppeteer/lib/python3.9/site-packages/pyppeteer/launcher.py", line 227, in get_ws_endpoint
    raise BrowserError('Browser closed unexpectedly:\n')
pyppeteer.errors.BrowserError: Browser closed unexpectedly:

How are you running the code? Is it in a Python console?

I found this related thread - https://www.pythonanywhere.com/forums/topic/28206/

Also this (looks like an option you need to include) - https://www.pythonanywhere.com/forums/topic/30330/

I'll be ding danged, that second one worked , thank you!

browser = await launch( args=['--no-sandbox'])

For future readers FYI, the processes take a few seconds to shut down so if you run it again too quickly, you get a crash

(I think)

Thanks for letting us know!