Alen Paul Varghese
Alen Paul Varghese

Reputation: 1542

Pyppeteer Browser closed unexpectedly in heroku

I recently deployed an app in heroku . It uses python pyppeteer package. I didnt had any issues while testing on repl.it. But unfortunately in heroku the browser keeps crashing.

I used requirement.txt for installing pyppeteer package. I also tried using apt heroku buildpack for installing the requirements needed for pupeteer to work as per here

My program :

async def mainer(link, path, is_image):
    browser = await launch(args=['--no-sandbox'])
    page = await browser.newPage()
    await page.goto(link)
    if is_image:
        await page.screenshot({'path': f'{path}', 'fullPage': True, 'type': 'png'})
    else:
        await page.pdf({'path': f'{path}'})
    await browser.close()

here is the full traceback error from heroku :

2020-05-14T19:39:50.115643+00:00 app[worker.1]:     await handler.callback(self.client, *args)
2020-05-14T19:39:50.115644+00:00 app[worker.1]:   File "/app/plugins/downloader.py", line 61, in cb_
2020-05-14T19:39:50.115645+00:00 app[worker.1]:     await mainer(url,file,mode)
2020-05-14T19:39:50.115645+00:00 app[worker.1]:   File "/app/plugins/downloader.py", line 13, in mainer
2020-05-14T19:39:50.115646+00:00 app[worker.1]:     browser = await launch(args=['--no-sandbox'])
2020-05-14T19:39:50.115646+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/pyppeteer/launcher.py", line 305, in launch
2020-05-14T19:39:50.115647+00:00 app[worker.1]:     return await Launcher(options, **kwargs).launch()
2020-05-14T19:39:50.115648+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/pyppeteer/launcher.py", line 166, in launch
2020-05-14T19:39:50.115648+00:00 app[worker.1]:     self.browserWSEndpoint = get_ws_endpoint(self.url)
2020-05-14T19:39:50.115648+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/pyppeteer/launcher.py", line 225, in get_ws_endpoint
2020-05-14T19:39:50.115649+00:00 app[worker.1]:     raise BrowserError('Browser closed unexpectedly:\n')
2020-05-14T19:39:50.115649+00:00 app[worker.1]: pyppeteer.errors.BrowserError: Browser closed unexpectedly:
2020-05-14T19:39:50.115650+00:00 app[worker.1]: 

Upvotes: 3

Views: 3626

Answers (3)

Top Topek
Top Topek

Reputation: 21

Using Django, when i call requests_html HTMLSession at start of my application i needed to add puppeteer-heroku-buildpack , heroku-buildpack-google-chrome as first buildspacks and heroku/python as last. In code had to add something like this: session = HTMLSession(browser_args=["--no-sandbox"])

Upvotes: 0

PAYRE Quentin
PAYRE Quentin

Reputation: 341

For Heroku Deployment I had this issue

pyppeteer.errors.BrowserError: Browser closed unexpectedly:

Following this, I just added '--no-sandbox' mode when launching Puppeteer and 'headless' set to True.

from pyppeteer import launch
    BROWSER = await launch({'headless': True, 'args': ['--no-sandbox']})

I added 2 buildpacks in my dyno settings:

enter image description here

This worked for me

Upvotes: 3

Alen Paul Varghese
Alen Paul Varghese

Reputation: 1542

If we care using the buildpack provided by Heroku, there will be an environmental variable called GOOGLE_CHROME_SHIM. We can use this variable as an argument to pyppeteer.launch() or webdriver.Chrome().

Upvotes: 3

Related Questions