Firefox in headless mode run in small size (1366, 768)
System
- Version: 0.21.0
- Platform: Windows 10, Microsoft Visual Studo 15.8 C#
- Firefox: Firefox Quantum 61.0.2 (64 bit)
- Selenium: 3.14.0
Testcase
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.AddArguments("--headless");
firefoxOptions.AddArguments("--window-size=1920,1080");
FirefoxDriver firefoxDriver = new FirefoxDriver(firefoxOptions);
firefoxDriver.Manage().Window.Maximize();
firefoxDriver.Navigate().GoToUrl("https://www.google.com/");
var size = firefoxDriver.Manage().Window.Size; //{Width = 1366 Height = 768}
Result:
Firefox run 1366 x 768 in headless mod
Expected:
Firefox should run in fullHD as I set.
Sorry, but what is the issue?
Firefox should run in 1920 x 1080 [firefoxOptions.AddArguments("--window-size=1920,1080");] but it is running in 1366 x 768 in headless mod
My suspicion is that firefoxOptions.AddArguments("--window-size=1920,1080") does not do what you expect. Could be that it needs to be split up into two calls to AddArguments, since = is a shell specific artifact.
firefoxOptions.AddArgument("--width=1920"); firefoxOptions.AddArgument("--height=1080"); https://github.com/SeleniumHQ/selenium/pull/6075
Thank you
firefoxOptions.AddArgument("--window-size").AddArgument("1920,1080") should also work.
The point is to avoid = because it’s shell-specific.
I see, thanks But as I see I got an another problem set firefox property Create firefox webdriver: FirefoxOptions firefoxOptions = new FirefoxOptions(); firefoxOptions.AddArguments("--headless"); firefoxOptions.AddArgument("--width=1920"); firefoxOptions.AddArgument("--height=1080"); FirefoxDriver firefoxDriver = new FirefoxDriver(firefoxOptions); var size = firefoxDriver.Manage().Window.Size; //{Width = 1920 Height = 1080}
and later... other code call this webDriver.Manage().Window.Maximize(); after this the resolution set back to 1366 x 768 which is smaller... (can avoid this method, because there are other webdriver settings) Maximize method while reduced the size?
Could you please attach a trace-level log from geckodriver for that?
@andreastt did you check the log file?
Could it be that the initial size of the Firefox window is greater than the window size used by headless? I actually don't know what's used in this case. If you maximize the window then, it will indeed by reduced to fit the whole available screen only.
No, the issue is not related to the display size. The 1366x768 resolution seems to be a hardcoded navigator property in headless mode. Here is a Python code reproducing the issue:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from pprint import pprint
options = Options()
options.add_argument('--headless')
driver = webdriver.Firefox(options=options)
driver.set_window_size(1920, 1080)
>>> driver.get_window_size()
{'width': 1920, 'height': 1080}
>>> pprint(driver.execute_script('return screen;'))
{'addEventListener': {},
'availHeight': 768,
'availLeft': 0,
'availTop': 0,
'availWidth': 1366,
'colorDepth': 24,
'dispatchEvent': {},
'height': 768,
'left': 0,
'mozLockOrientation': {},
'mozOrientation': 'landscape-primary',
'mozUnlockOrientation': {},
'onmozorientationchange': None,
'orientation': {'addEventListener': {},
'angle': 0,
'dispatchEvent': {},
'lock': {},
'onchange': None,
'removeEventListener': {},
'type': 'landscape-primary',
'unlock': {}},
'pixelDepth': 24,
'removeEventListener': {},
'top': 0,
'width': 1366}
Note that despite the reported window size is 1920x1080, internal screen properties remain at 1366x768. It's a pretty bad issue: first, it breaks maximize_window() call causing the resolution to cap to 1366x768; second and most importantly, it breaks website scripts which rely on screen properties.
Needless to say, this doesn't happen in GUI mode.
from selenium import webdriver
driver = webdriver.Firefox()
driver.set_window_size(1920, 1080)
>>> driver.get_window_size()
{'width': 1920, 'height': 1080}
>>> screen = driver.execute_script('return screen;')
>>> screen['width'], screen['height']
(1920, 1080)
Depending on the window manager in use and operating system the window can be larger than the screen. As such its dimensions will also be greater than what screen returns. I assume that your screen has 1920x1080? Try your last example with setting a window size like 2100x1200.
I'm probably missing your point, but can't see what this is supposed to show. The screen object contains properties of the OS/WM, yes, and running the last example with changed window size the screen still returns 1920x1080 (as it's a system property) while the browser window exceeds the screen size.
But this is besides my point. The point is the headless mode (geckodriver or some component it's talking to) ignores the OS/WM screen size completely, making the screen report wrong values:
>>> from selenium import webdriver
>>> from selenium.webdriver.firefox.options import Options
>>> options = Options()
>>> options.add_argument('--headless')
>>> driver = webdriver.Firefox(options=options)
>>> driver.set_window_size(2100, 1200)
>>> driver.get_window_size()
{'width': 2100, 'height': 1200}
>>> screen = driver.execute_script('return screen;')
>>> screen['width'], screen['height']
(1366, 768)
Can you suggest where this 1366x768 resolutions is coming from?
Headless mode uses a virtual display which means it doesn’t derive or reflect any aspects of the display it’s hosted in.
That window.screen reports an inaccurate representation of that display is likely a bug. geckodriver has had synchronisation issues with the Marionette:SetWindowRect command in the past, but I believe these are resolved, and in any case shouldn’t manifest under headless mode where we don’t suffer from the WM being intrinsicly asynchronous, as the size information is basically just stored in a struct.
If you can reproduce this with Firefox Nightly, it may be worth filing a bug under Firefox :: Headless.
1366x768 is the default resolution for headless mode. You can change this by setting the following environment variables: MOZ_HEADLESS_WIDTH and MOZ_HEADLESS_HEIGHT.
Thank you, this explains a lot. I can confirm that using these variables I'm getting the expected results (both regarding the maximize_window method and the screen object properties):
>>> import os
>>> os.environ['MOZ_HEADLESS_WIDTH'] = '1920'
>>> os.environ['MOZ_HEADLESS_HEIGHT'] = '1080'
>>> from selenium import webdriver
>>> from selenium.webdriver.firefox.options import Options
>>> from pprint import pprint
>>> options = Options()
>>> options.add_argument('--headless')
>>> driver = webdriver.Firefox(options=options, firefox_binary='/tmp/1/firefox/firefox-bin')
>>> driver.maximize_window()
>>> driver.get_window_size()
{'width': 1920, 'height': 1080}
>>> screen = driver.execute_script('return screen;')
>>> screen['width'], screen['height']
(1920, 1080)
Good. And that also demonstrates that in headless mode Firefox allows to set a window size which is larger than the screen size.
This issue has been automatically locked since there has not been any recent activity after it was closed. If you have run into an issue you think is related, please open a new issue.