Running browser automation without GUI on Heroku platform

I’m working on a web automation project that needs to run on Heroku. My team built a script that uses Selenium WebDriver to control Firefox browser, but we’re running into issues when deploying to Heroku since it doesn’t have a display.

The script works perfectly on our local machines where we can see the browser window opening and performing actions. However, when we try to deploy this same code to Heroku, it fails because there’s no graphical interface available.

I’m looking for advice on how to modify our existing Selenium setup to work in a server environment without a display. Are there specific browser configurations or WebDriver options we can add to make this work? We’d prefer to keep our current code structure and just add the necessary settings to make it compatible with headless execution.

Any suggestions or examples would be really helpful. We’re open to switching browsers if needed, but want to avoid rewriting the entire automation logic.

To run your Selenium setup on Heroku without a GUI, you must configure Firefox to run in headless mode. This involves adding the ‘–headless’ option to your WebDriver setup and ensuring you’re using the Firefox buildpack for your Heroku app. Make sure to also include display dimensions since headless browsers require defined window sizes. Set the environment variable ‘MOZ_HEADLESS’ to ‘1’ in your Heroku config as well. Keep an eye on dyno memory usage, as headless browsers can consume considerable RAM. If you encounter issues, consider switching to Chrome, which often provides better stability in headless scenarios.

Had this exact issue when I deployed my scraper to Heroku last year. You’ll need more than just the headless flag - there’s some tricky config stuff that’s not obvious. Install the Firefox binary through the heroku buildpack and set your PATH variables right. Default Firefox profiles don’t play nice with containers, so you might need a custom profile or just disable sandboxing. Heroku’s ephemeral filesystem messes with Firefox’s temp files too - I set a custom temp directory to fix that. Oh, and the memory limits on free dynos are brutal. I had to close browser instances way more often than I did locally.

chrome’s way easier than firefox for this. just add --headless --no-sandbox --disable-dev-shm-usage to your chrome options and use the google chrome buildpack. less config headaches and runs smoother on heroku’s containers.