3

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]: 
Alen Paul Varghese
  • 1,278
  • 14
  • 27

3 Answers3

3

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

PAYRE Quentin
  • 341
  • 1
  • 12
2

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().

Alen Paul Varghese
  • 1,278
  • 14
  • 27
0

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"])

Top Topek
  • 11
  • 2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/31248267) – user11717481 Mar 12 '22 at 03:42