How to run headless Firefox in the window size (1920, 1080) in C#?

5.1k views Asked by At

How can I run headless Firefox in the window size (1920, 1080) in C#?

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}

Any suggestions, how to run in full HD? I need to set here...

For example with chrome working correctly:

ChromeOptions options = new ChromeOptions();
options.AddArguments("headless");
options.AddArguments("window-size=1920x1080");
4

There are 4 answers

0
Zain UL Abdeen On

I am able to run firefox without GUI on ubuntu after a couple of search, here you can try

# install Xvfb (X Virtual Frame Buffer) and related Fonts 
$ sudo apt-get install xvfb 
$ sudo apt-get install xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic 

# Setup Display 
$ export DISPLAY=:1 

# run Xvfb in background, set screen resolution as HDMI (1920x1080, 24 dpi), run on display:1 
$ Xvfb :1 -screen 0 1920x1080x24 &

# Run firefox
firefox &

# OR
firefox --headless &
2
Abhishek_Mishra On

Try using the Dimension Class after driver object creation.

Dimension d = new Dimension(1920,1080);
driver.manage().window().setSize(d);
0
Smith On

I find it:

firefoxOptions.AddArguments("-width=1920");
firefoxOptions.AddArguments("-height=1080");

but if you call somewhere in your code this:

webDriver.Manage().Window.Maximize();

then set to default resolution.. {Width = 1366 Height = 768}

0
Super Kai - Kazuya Ito On

<Chrome>

*-window-size and window-size also work:

ChromeOptions options = new ChromeOptions();
options.AddArguments("--window-size=1920,1080");
ChromeDriver driver = new ChromeDriver(options);

*This code below also works according to the doc:

ChromeDriver driver = new ChromeDriver();
driver.Manage().Window.Size = new Size(1920, 1080);

<Microsoft Edge>

*-window-size and window-size also work:

EdgeOptions options = new EdgeOptions();
options.AddArguments("--window-size=1920,1080");
EdgeDriver driver = new EdgeDriver(options);

*This code below also works according to the doc:

EdgeDriver driver = new EdgeDriver();
driver.Manage().Window.Size = new Size(1920, 1080);

<Firefox>

*-width and -height also work while width and height don't work:

FirefoxOptions options = new FirefoxOptions();
options.AddArguments("--width=1920");
options.AddArguments("--height=1080");
FirefoxDriver driver = new FirefoxDriver(options);

*This code below also works according to the doc:

FirefoxDriver driver = new FirefoxDriver();
driver.Manage().Window.Size = new Size(1920, 1080);