views:

148

answers:

3

Is there a way to maximize the browser window using WebDriver (Selenium 2)?

+3  A: 

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);");
}
Dave Hunt
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
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
+1  A: 

You can use Selenium Emulation in WebDriver:

selenium = new WebDriverBackedSelenium(driver,url);
selenium.windowMaximize();
ZloiAdun
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
A: 

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