Home > Net >  Error: no DISPLAY environment variable specified; Selenium webdriver options have no effect
Error: no DISPLAY environment variable specified; Selenium webdriver options have no effect

Time:02-04

I'm running this on arm64 ubuntu 20.04 lts with python3-selenium installed. After specifying --headless arguments and running this code:

from selenium import webdriver
opt = webdriver.FirefoxOptions()
opt.add_argument("--headless")
serv = Service("/usr/local/bin/firefox/geckodriver")
driver = webdriver.Firefox(service=serv, firefox_options=opt)
driver.get(site)

I still get the error:

selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 1

Some further digging into the geckodriver.log file

mozrunner::runner   INFO    Running command: "/usr/bin/firefox" "--marionette" "-no-remote" "-profile" "/tmp/rust_mozprofilewMNmVf"
Error: no DISPLAY environment variable specified

Oddly enough, none of the arguments I specified are is being run here. Any ideas?

Using the exact same code with webdriver.Chrome instead of webdriver.Firefox works. Unfortunately, I need this to work with firefox.

CodePudding user response:

For Firefox to be headless:

opt.headless = True

or

opt.set_headless()

CodePudding user response:

I figured it out. Apparently you're supposed to set

driver = webdriver.Firefox(service=serv, options=opt)

NOT

driver = webdriver.Firefox(service=serv, firefox_options=opt)

Oddly enough, both parameters are available in webdriver.Firefox

from selenium import webdriver
opt = webdriver.FirefoxOptions()
opt.add_argument("--headless")
serv = Service("/usr/local/bin/firefox/geckodriver")
driver = webdriver.Firefox(service=serv, options=opt)
driver.get(site)

  •  Tags:  
  • Related