Skip to content

Commit 7fe3306

Browse files
committed
Fix bug with headless Firefox on Linux without Xvfb
1 parent b3c995f commit 7fe3306

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

seleniumbase/core/browser_launcher.py

+29-7
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,11 @@ def _set_firefox_options(
847847
)
848848
if headless and not IS_LINUX:
849849
options.add_argument("--headless")
850+
elif headless and IS_LINUX:
851+
# This assumes Xvfb is running, which prevents many Linux issues.
852+
# If not, we'll fix this later during the error-handling process.
853+
# To override this feature: ``pytest --firefox-arg="-headless"``.
854+
pass
850855
if locale_code:
851856
options.set_preference("intl.accept_languages", locale_code)
852857
options.set_preference("browser.shell.checkDefaultBrowser", False)
@@ -1972,10 +1977,18 @@ def get_local_driver(
19721977
or "A connection attempt failed" in e.msg
19731978
)
19741979
):
1975-
# Firefox probably just auto-updated itself,
1976-
# which causes intermittent issues to occur.
1977-
# Trying again right after that often works.
19781980
time.sleep(0.1)
1981+
if (
1982+
IS_LINUX
1983+
and headless
1984+
and (
1985+
"unexpected" in str(e)
1986+
or (
1987+
hasattr(e, "msg") and "unexpected" in e.msg
1988+
)
1989+
)
1990+
):
1991+
firefox_options.add_argument("-headless")
19791992
return webdriver.Firefox(
19801993
service=service,
19811994
options=firefox_options,
@@ -1993,7 +2006,8 @@ def get_local_driver(
19932006
service = FirefoxService(log_path=os.devnull)
19942007
try:
19952008
return webdriver.Firefox(
1996-
service=service, options=firefox_options
2009+
service=service,
2010+
options=firefox_options,
19972011
)
19982012
except BaseException as e:
19992013
if (
@@ -2008,10 +2022,18 @@ def get_local_driver(
20082022
or "A connection attempt failed" in e.msg
20092023
)
20102024
):
2011-
# Firefox probably just auto-updated itself,
2012-
# which causes intermittent issues to occur.
2013-
# Trying again right after that often works.
20142025
time.sleep(0.1)
2026+
if (
2027+
IS_LINUX
2028+
and headless
2029+
and (
2030+
"unexpected" in str(e)
2031+
or (
2032+
hasattr(e, "msg") and "unexpected" in e.msg
2033+
)
2034+
)
2035+
):
2036+
firefox_options.add_argument("-headless")
20152037
return webdriver.Firefox(
20162038
service=service,
20172039
options=firefox_options,

0 commit comments

Comments
 (0)