157

Is there any way to maximize the browser window using WebDriver (Selenium 2) with C#?

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Reflux
  • 2,929
  • 4
  • 26
  • 27

29 Answers29

186

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);
Dirk
  • 3,030
  • 1
  • 34
  • 40
bipinkarms
  • 2,059
  • 2
  • 13
  • 6
60

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
Community
  • 1
  • 1
Prashanth Sams
  • 19,677
  • 20
  • 102
  • 125
45

For IE and Firefox:

driver.manage().window().maximize();

For Chrome:

ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
driver = new ChromeDriver( options )
Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
Luis
  • 551
  • 4
  • 4
43

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);");
}
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Dave Hunt
  • 8,191
  • 4
  • 38
  • 39
  • 13
    Two 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
  • 4
    Note 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
  • Should be JavascriptExecutor instance IJavaScriptExecutor – TuGordoBello May 18 '17 at 15:05
20

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);
Ripon Al Wasim
  • 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
  • swanhella: Thanks to share this – Ripon Al Wasim Jun 07 '17 at 10:27
  • "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
15

You can use something like this (C#):

driver.Manage().Window.Size = new Size(1024, 768);
DrunkCoder
  • 8,215
  • 3
  • 19
  • 14
  • 2
    Unfortunately 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
8

Simply use Window.Maximize() command

WebDriver driver= new ChromeDriver()
driver.Manage().Window.Maximize();  
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
8

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);
Joey V.
  • 1,866
  • 1
  • 22
  • 18
6

For Java:

driver.manage().window().maximize();

It will work in IE, Mozilla, Chrome

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Ramaguru N
  • 69
  • 1
  • 1
6

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);
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
KrishPrabakar
  • 2,824
  • 2
  • 31
  • 44
5

I had the same problem, but the problem can be solved by using following code.

driver.manage().window().fullscreen();
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
skarki
  • 87
  • 2
  • 6
4

For C#:

driver.Manage().Window.Maximize();

For Java:

driver.manage().window().maximize();
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
4

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);
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
3
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.

2

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);
Coder323
  • 580
  • 6
  • 17
2

You can use Selenium Emulation in WebDriver:

selenium = new WebDriverBackedSelenium(driver,url);
selenium.windowMaximize();
Sergii Pozharov
  • 17,366
  • 4
  • 29
  • 30
  • 1
    They 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
  • And it also doesn't support iPhoneDriver, for example – Iulius Curt Jul 27 '11 at 12:25
  • 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
2

For Chrome

ChromeOptions options = new ChromeOptions();
IWebDriver driver = new ChromeDriver(options);
driver.Manage().Window.Maximize();
1

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');
Anup
  • 600
  • 3
  • 9
1

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()
fixxxer
  • 15,568
  • 15
  • 58
  • 76
1

This option is fine for me :

ChromeOptions options = new ChromeOptions();
options.addArguments("start-fullscreen");

This option works on all OS.

Sagar007
  • 848
  • 1
  • 13
  • 33
1

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"

spartan
  • 51
  • 3
1

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

JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
1

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);
0

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);
    };");
Perception
  • 79,279
  • 19
  • 185
  • 195
Rajagopal
  • 9
  • 2
0

This is working fine for me.

Capybara.current_session.driver.browser.manage.window.resize_to(1800, 1000)
suresh gopal
  • 3,138
  • 6
  • 27
  • 58
0

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.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
0

Chrome driver already support:

Java:

webDriver = new ChromeDriver();
webDriver.manage().window().maximize();
Viktor Chmel
  • 145
  • 7
0

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);
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
0

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);
Houssin Boulla
  • 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