geckodriver icon indicating copy to clipboard operation
geckodriver copied to clipboard

Firefox in headless mode run in small size (1366, 768)

Open Lamasd opened this issue 8 years ago • 20 comments

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.

Lamasd avatar Aug 15 '18 19:08 Lamasd

Sorry, but what is the issue?

andreastt avatar Aug 16 '18 09:08 andreastt

Firefox should run in 1920 x 1080 [firefoxOptions.AddArguments("--window-size=1920,1080");] but it is running in 1366 x 768 in headless mod

Lamasd avatar Aug 16 '18 11:08 Lamasd

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.

andreastt avatar Aug 16 '18 13:08 andreastt

firefoxOptions.AddArgument("--width=1920"); firefoxOptions.AddArgument("--height=1080"); https://github.com/SeleniumHQ/selenium/pull/6075

Thank you

Lamasd avatar Aug 16 '18 13:08 Lamasd

firefoxOptions.AddArgument("--window-size").AddArgument("1920,1080") should also work.

andreastt avatar Aug 16 '18 13:08 andreastt

The point is to avoid = because it’s shell-specific.

andreastt avatar Aug 16 '18 13:08 andreastt

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?

Lamasd avatar Aug 16 '18 16:08 Lamasd

Could you please attach a trace-level log from geckodriver for that?

andreastt avatar Aug 16 '18 17:08 andreastt

trace_log.txt Program.txt

Log file attached!

Lamasd avatar Aug 16 '18 18:08 Lamasd

@andreastt did you check the log file?

Lamasd avatar Aug 23 '18 18:08 Lamasd

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.

whimboo avatar Aug 24 '18 08:08 whimboo

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.

sattva1 avatar Mar 19 '19 16:03 sattva1

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)

sattva1 avatar Mar 19 '19 17:03 sattva1

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.

whimboo avatar Apr 02 '19 09:04 whimboo

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?

sattva1 avatar Apr 02 '19 10:04 sattva1

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.

andreastt avatar Apr 03 '19 10:04 andreastt

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.

whimboo avatar Apr 03 '19 11:04 whimboo

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)

sattva1 avatar Apr 03 '19 12:04 sattva1

Good. And that also demonstrates that in headless mode Firefox allows to set a window size which is larger than the screen size.

whimboo avatar Apr 03 '19 12:04 whimboo

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.

lock[bot] avatar Aug 16 '19 11:08 lock[bot]