Troubleshooting headless browser issues on my Raspberry Pi

I’m having trouble getting a headless browser to function properly on my Raspberry Pi B+. I’m using a Python script but it’s still opening the browser in normal mode instead of headless. I’ve checked my setup, but I can’t seem to find what I’m missing.

Here’s the code I’m working with:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options

import time

options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=options, executable_path=r'/usr/local/bin/geckodriver')

driver.get('https://www.google.com')

Although I don’t see any errors, the headless mode isn’t activating and the browser keeps appearing on my screen. Does anyone have experience with this on a Raspberry Pi and can provide some insight?

Had the same issue when setting up automated scraping on my Pi 3. Your problem is the deprecated firefox_options parameter. Firefox WebDriver switched to just options in newer Selenium versions. Replace firefox_options=options with options=options in your webdriver.Firefox() call. Also check if your geckodriver version matches your Firefox version - I had to downgrade Firefox once because they weren’t compatible. One more thing: make sure you don’t have a display server running that’s forcing the browser to show up even with the headless flag.

set the display variable before importing selenium - just run export DISPLAY="" at the start of your terminal session. also bump up your pi’s gpu memory split to 128MB using sudo raspi-config. firefox gets weird on pi’s with default memory settings, even headless.

This might be your X11 forwarding messing things up if you’re SSH’d into the Pi. Even with headless enabled, Firefox can still pop up if X11 forwarding’s active and your local machine has a display available. Try DISPLAY='' python your_script.py to force no display environment. I hit this exact same issue testing browser automation remotely - headless flag got ignored because of the forwarded display. Also try adding options.add_argument('--disable-gpu') since that sometimes fixes headless mode on Pi ARM devices.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.