Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Playwright fails to launch Chromium on Heroku despite executable being present #26

Open
justinsane opened this issue Sep 20, 2023 · 2 comments

Comments

@justinsane
Copy link

Description

I'm facing an issue while deploying a Python app on Heroku that utilizes Playwright to launch Chromium. Despite following best practices and ensuring the Chromium executable is present in the expected directory, I consistently receive an error stating that the "executable doesn't exist".

Env Details
Platform: Heroku
Buildpacks (in order):
mxschmitt/heroku-playwright-buildpack
heroku/python
Python V: 3.11
Playwright V: 1.36.0
Heroku Stack: heroku-22
Relevant Code:

with sync_playwright() as p:
  # Troubleshooting for local and Heroku build
  default_path = "/app/.heroku/python/lib/python3.11/site-packages/playwright/driver/package/.local-browsers/chromium-1071/chrome-linux/chrome"
  chromium_path = os.getenv("PLAYWRIGHT_CHROMIUM_PATH", default_path)

  browser = p.chromium.launch(
    headless=True, # Set False for debugging and local dev
    args=['--no-sandbox'],
    executable_path=chromium_path
      )
   # ... rest of code

Error Message
playwright._impl._api_types.Error: Failed to launch chromium because executable doesn't exist at /app/.heroku/python/lib/python3.11/site-packages/playwright/driver/package/.local-browsers/chromium-1071/chrome-linux/chrome

However, running heroku run bash and verify presence of the executable using ls, I can clearly see the Chromium executable present at the mentioned path.

Steps Taken:

Verified the presence of the Chromium executable on the Heroku Dyno.

Ensured the Playwright buildpack is correctly set up and is first in the order.

Tried using different environment variables including PLAYWRIGHT_BROWSERS_PATH and PLAYWRIGHT_CHROMIUM_PATH.

Checked for any potential conflicts with Selenium (previously used in the project) and its associated environment variables.

Expected
Playwright should successfully launch Chromium without any errors.

Actual
Playwright consistently fails to launch Chromium with an error stating the executable doesn't exist, even though it's present in the directory.

@justinsane
Copy link
Author

This solution on Stack Overflow helped me - Stack Overflow

Heroku's filesystem is both ephemeral and dyno-local. Any changes made to it are lost when the dyno restarts. More importantly at the moment, when you run something like heroku run bash you don't connect to a running dyno. Instead, you get a separate one-off dyno.

Installing Chromium (or making any other filesystem changes) in such an environment will never affect what is available in your other dynos.

It looks like Playwright really wants to manage its own browser binaries. Your best bet might be to run playwright install near the beginning of your script, e.g. something like this:

import subprocess
from playwright.sync_api import sync_playwright

subprocess.run(["playwright", "install"])

with sync_playwright() as playwright:
    # ...

This should make sure that the required binaries are always available at runtime.

@Thomas-Boi
Copy link

I recently made a buildpack to address this issue. I did made it to use Chromium but it should work with Firefox and Webkit as well. Feel free to give it a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants