Is there any way to maximize the browser window using WebDriver (Selenium 2) with C#?
-
1WebDriver (http://code.google.com/p/selenium/?redir=1) basically allows you to test web pages though code. – Reflux Jul 07 '10 at 22:19
-
1Selenium WebDriver is the successor of Selenium RC – Ripon Al Wasim Oct 09 '13 at 06:32
29 Answers
driver.Manage().Window.Maximize();
This works for IE and Firefox. Chrome does not work. There is a bug submitted for this on ChromeDriver project.
Meanwhile, the get around for the chrome is to implement what Joey V. and Coder323 suggested.
ChromeOptions options = new ChromeOptions();
options.addArgument("--start-maximized");
driver = new ChromeDriver(options);

- 3,030
- 1
- 34
- 40

- 2,059
- 2
- 13
- 6
-
1This way for firefox is the only one that works, the javascript doesnt do anythign on selenium 2.21 – Sam Adamsh May 11 '12 at 08:12
-
6
-
-
-
I am using FF53 with Selenium WebDriver 3.3.0 . driver.Manage().Window.Maximize(); works for me – Ripon Al Wasim Mar 27 '17 at 13:37
-
-
It worked in chrome for me. `var driver =new ChromeDriver(); driver.Manage().Window.Maximize();` – Atiq Baqi Nov 14 '20 at 11:16
-
This appears to be fixed in ChromeDriver now without adding the --start-maximized argument. – John Chesshir Apr 19 '22 at 21:04
Java
driver.manage().window().maximize();
Python
driver.maximize_window()
Ruby
@driver.manage.window.maximize
OR
max_width, max_height = driver.execute_script("return [window.screen.availWidth, window.screen.availHeight];")
@driver.manage.window.resize_to(max_width, max_height)
OR
target_size = Selenium::WebDriver::Dimension.new(1600, 1268)
@driver.manage.window.size = target_size

- 1
- 1

- 19,677
- 20
- 102
- 125
For IE and Firefox:
driver.manage().window().maximize();
For Chrome:
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
driver = new ChromeDriver( options )

- 6,602
- 11
- 44
- 59

- 551
- 4
- 4
-
7
-
-
Thanks Chris, C# needs uppercase methods : this. driver.Manage().Window.Maximize(); – LA Guy 88 Mar 05 '17 at 20:05
There's an outstanding issue to add this functionality to WebDriver, which can be tracked here: http://code.google.com/p/selenium/issues/detail?id=174
A workaround would be to use the JavascriptExector
as follows:
public void resizeTest() {
driver.Navigate().GoToUrl("http://www.example.com/");
((IJavaScriptExecutor)driver).ExecuteScript("window.resizeTo(1024, 768);");
}

- 36,924
- 42
- 155
- 176

- 8,191
- 4
- 38
- 39
-
13Two things: 1) If you care what's visible you probably want to move the window to origin before making it full screen. 2) If the screen dimensions are variable you can get them at run time with the javascript `screen` object. Taking these points into consideration the javascript should probably be: `window.moveTo(0,0); window.resizeTo(screen.width, screen.height);` – Guildencrantz Jul 08 '10 at 01:30
-
4Note that there's an issue moving the window to (0,0) in Firefox on Mac. It appears to not account for its header region properly. Moving the window to (0,1) seems to actually move it to (0,0) though, so one bug helps another. At that point you can maximize the window properly. – nirvdrum Aug 12 '10 at 11:58
-
Test step/scenario:
1. Open a browser and navigate to TestURL
2. Maximize the browser
Maximize the browser with C# (.NET):
driver.Manage().Window.Maximize();
Maximize the browser with Java :
driver.manage().window().maximize();
Another way to do with Java:
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenResolution = new Dimension((int)
toolkit.getScreenSize().getWidth(), (int)
toolkit.getScreenSize().getHeight());
driver.manage().window().setSize(screenResolution);

- 36,924
- 42
- 155
- 176
-
This fails in case of chrome docker container with error java.awt.HeadlessException at sun.awt.HeadlessToolkit.getScreenSize(HeadlessToolkit.java:284). Did you encounter this issue before ? – vikramvi Oct 19 '16 at 19:48
-
I tried with driver.Manage().Window.Maximize(); in Selenium WebDriver C# on FF and it worked well. I didn't try with Chrome yet. – Ripon Al Wasim Apr 09 '17 at 10:34
-
This worked for me but I had to add `driver.manage().window().setPosition(new Point(0,0));` before the last line or the window wouldn't be in the right position. – swanhella Jun 07 '17 at 00:34
-
-
"Another way to do with Java:" helped me alot, in my case selenium didn't screenshot all screen and setting size fixed the problem. – Jeffrey Jul 03 '22 at 10:43
You can use something like this (C#):
driver.Manage().Window.Size = new Size(1024, 768);

- 8,215
- 3
- 19
- 14
-
2Unfortunately this throws and exception for the ChromeDriver. Works fine for IE and Firefox though. – Bryan Allred Mar 27 '12 at 15:00
-
Using this code with Selenium C# should work fine now since ChromeDriver is now provided by Google (though it 's for selenium 3). Alternatively you could use the maximize function or just add the following argument via options: "--start-maximized" – user1841243 Jul 01 '19 at 14:08
Simply use Window.Maximize()
command
WebDriver driver= new ChromeDriver()
driver.Manage().Window.Maximize();

- 4,607
- 2
- 15
- 36
If you are using the Chrome Driver you can set the capabilities
var capabilities = new DesiredCapabilities();
var switches = new List<string>
{
"--start-maximized"
};
capabilities.SetCapability("chrome.switches", switches);
new ChromeDriver(chromedriver_path, capabilities);

- 1,866
- 1
- 22
- 18
For Java:
driver.manage().window().maximize();
It will work in IE, Mozilla, Chrome

- 36,924
- 42
- 155
- 176

- 69
- 1
- 1
The following Selenium Java code snippet worked for me:
driver.manage().window().setPosition(new Point(0,0));
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
Dimension dim = new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
driver.manage().window().setSize(dim);

- 36,924
- 42
- 155
- 176

- 2,824
- 2
- 31
- 44
I had the same problem, but the problem can be solved by using following code.
driver.manage().window().fullscreen();

- 47,830
- 31
- 106
- 135

- 87
- 2
- 6
For C#:
driver.Manage().Window.Maximize();
For Java:
driver.manage().window().maximize();

- 36,924
- 42
- 155
- 176

- 527
- 7
- 20
We have observed bug with new driver libraries. You can use slightly old jars which are able to handle new browsers versions.
The main generic option is :-
driver.manage().window().maximize();
You can also use other option for maximizing the browser window.
Example:-
Add below option and pass it to driver:-
chromeOptions.addArguments("--start-maximized");
The full code will look like below :-
System.setProperty("webdriver.chrome.driver","D:\\Workspace\\JmeterWebdriverProject\\src\\lib\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--start-maximized");
driver = new ChromeDriver(chromeOptions);
OR
Toolkit toolkit = Toolkit.getDefaultToolkit();
int Width = (int) toolkit.getScreenSize().getWidth();
int Height = (int)toolkit.getScreenSize().getHeight();
//For Dimension class, Import following library "org.openqa.selenium.Dimension"
driver.manage().window().setSize(new Dimension(Width,Height));
driver.get("https://google.com");
Try this on safari :-
JavascriptExecutor jse = (JavascriptExecutor)driver;
String screenWidth = jse.executeScript("return screen.availWidth").toString();
String screenHeight = jse.executeScript("return screen.availHeight").toString();
int intScreenWidth = Integer.parseInt(screenWidth);
int intScreenHeight = Integer.parseInt(screenHeight);
Dimension d = new Dimension(intScreenWidth, intScreenHeight);
driver.manage.window.setSize(d);

- 16,610
- 15
- 78
- 125
using System.Windows.Forms;
using System.Drawing;
public static void MaximizeBrowser(this IE myBrowser)
{
myBrowser.SizeWindow(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
}
I used Jim's code, but slightly modified for use with WatiN and C# to maximize the browser.

- 63
- 4
I used this solution
OpenQA.Selenium.Chrome.ChromeOptions chromeoptions = new OpenQA.Selenium.Chrome.ChromeOptions();
chromeoptions.AddArgument("--start-maximized");
OpenQA.Selenium.Chrome.ChromeDriver chrome = new OpenQA.Selenium.Chrome.ChromeDriver(chromeoptions);

- 580
- 6
- 17
You can use Selenium Emulation in WebDriver:
selenium = new WebDriverBackedSelenium(driver,url);
selenium.windowMaximize();

- 17,366
- 4
- 29
- 30
-
1They don't have Selenium Emulation in the C# version of webdriver. At least not yet in 2.0a4 (just saw they released a new version haven't checked it out yet though). However the above javascript code works great. – Reflux Jul 16 '10 at 18:55
-
-
Unfortunately WebDriverBackedSelenium.windowMaximize() doesn't work in Firefox 7 or Chrome 14 (although it does work in IE8). Behind the scenes it issues a window.resize() Javascript command, but both these browsers ignore it. @joey-v's solution works for Chrome. And Luke Inman-Semerau posted a solution [here](http://groups.google.com/group/webdriver/msg/dd29f1b1d1f55dc7); – Andrew Newdigate Oct 26 '11 at 11:23
For Chrome
ChromeOptions options = new ChromeOptions();
IWebDriver driver = new ChromeDriver(options);
driver.Manage().Window.Maximize();

- 31
- 3
For Webdriverjs (node.js), the following maximizes chrome window.
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).build();
driver.manage().window().maximize();
driver.get('hxxp://localhost:8888');

- 600
- 3
- 9
There is a function that you can use to maximize the window in Python which is window_maximize(). And this is how I'm using it.Hope this helps -
from selenium import selenium
sel = selenium('localhost', 4444, '*firefox', 'http://10.77.21.67/')
sel.start()
sel.open('/')
sel.wait_for_page_to_load(60000)
#sel.window_focus()
sel.window_maximize()
png = sel.capture_screenshot_to_string()
f = open('screenshot.png', 'wb')
f.write(png.decode('base64'))
f.close()
sel.stop()

- 15,568
- 15
- 58
- 76
This option is fine for me :
ChromeOptions options = new ChromeOptions();
options.addArguments("start-fullscreen");
This option works on all OS.

- 848
- 1
- 13
- 33
The below line of code would maximize IE, Chrome and Mozilla
driver.manage().window().maximize();
The above line of code and other workarounds mentioned in the post did not work for NodeWebKit browser, so as a workaround i had to use native C# code as mentioned below:
public static void MaximiseNWKBrowser(IWebDriver d)
{
var body = UICommon.GetElement(By.TagName("body"), d);
body.Click();
string alt = "%";
string space = " ";
string down = "{DOWN}";
string enter = "{ENTER}";
SendKeys.SendWait(alt + space);
for(var i = 1; i <= 6; i++)
{
SendKeys.SendWait(down);
}
SendKeys.SendWait(enter);
}
So this workaround basically uses "ALT+SPACE" to bring up the browser action menu to select "MAXIMIZE" from the options and presses "ENTER"

- 51
- 3
C# client drivers:
driver = new FirefoxDriver(firefoxProfile);
driver.Manage().Window.Size = new System.Drawing.Size(System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width+10, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height+10);
===> also add a reference to the .NET assembly "System.Windows.Forms"
... the only problem is that it's not positioned correctly
... please comment if you can correct this

- 18,332
- 38
- 160
- 245
Here's what worked for me in C#, firefoxDriver is global to the class:
in the usings:
using System.Drawing;
using System.Windows.Forms;
in the code:
this.firefoxDriver = new FirefoxDriver();
this.firefoxDriver.Manage().Window.Position = new Point(0, 0);
this.firefoxDriver.Manage().Window.Size = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);

- 11
- 1
Through the below code i'm able to maximize the window,
((JavascriptExecutor) driver).executeScript("if(window.screen){
window.moveTo(0, 0);
window.resizeTo(window.screen.availWidth, window.screen.availHeight);
};");

- 79,279
- 19
- 185
- 195

- 9
- 2
This is working fine for me.
Capybara.current_session.driver.browser.manage.window.resize_to(1800, 1000)

- 3,138
- 6
- 27
- 58
I tried many of the answers above, but none work well. My chrome driver version is 2.7 and Iam using selenium-java vesion is 2.9.0. The official document suggests using:
var capabilities = new DesiredCapabilities();
var switches = new List<string>
{
"--start-maximized"
};
capabilities.SetCapability("chrome.switches", switches);
new ChromeDriver(chromedriver_path, capabilities);
The above also does not work. I checked the chrome driver JsonWireProtocol
:
http://code.google.com/p/selenium/wiki/JsonWireProtocol
The chrome diver protocol provides a method to maximize the window:
/session/:sessionId/window/:windowHandle/maximize,
but this command is not used in selenium-java. This means you also send the command to chrome yourself. Once I did this it works.

- 59,888
- 27
- 145
- 179
Chrome driver already support:
Java:
webDriver = new ChromeDriver();
webDriver.manage().window().maximize();

- 145
- 7
For me, none of the solutions above worked when working with Selenium Web Driver C# + Chrome:
- window.resizeTo(1024, 768); - I want to use the whole screen
--start-maximized
- it is ignored- driver.manage().window().maximize(); - does not work because it requires some extension and I am not allowed to use Chrome extensions
I managed to get it working using InputSimulator:
var inputSim = new InputSimulator();
// WinKey + UP = Maximize focused window
inputSim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.LWIN, VirtualKeyCode.UP);

- 22,016
- 16
- 145
- 164
You can try with this code to maximize chrome window.
ChromeOptions options = new ChromeOptions();
options.addArguments("--window-size=1920,1080");
WebDriver driver = new ChromeDriver(options);

- 2,687
- 1
- 16
- 22
-
While it’s acceptable to provide code-only answers, it’s often more useful for the community if you can also provide an explanation of the code and help people understand why it addresses the problem. That often reduces the amount of follow-up questions, and can really help new developers understand the underlying concepts. That's especially important here, where there's already 27 other answers; it's worth calling out what sets your approach apart from the existing answers. Would you mind updating your question with additional detail? – Jeremy Caney May 08 '20 at 18:20