Window Maximize using Selenium webdriver v2.7.0

2,857 views
Skip to first unread message

TCBlues

unread,
Sep 29, 2011, 9:48:13 AM9/29/11
to webdriver
I'm trying to maximize the window but with no success


require "selenium-webdriver"
driver = Selenium::WebDriver.for :chrome
driver.navigate.to "http://www.google.es"
driver.window_maximize

Soujanya R

unread,
Sep 29, 2011, 9:56:19 AM9/29/11
to webd...@googlegroups.com
Did you try giving window size like below?

((IJavaScriptExecutor)driver).ExecuteScript("window.resizeTo(1280,1024);");


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.


Moises Siles

unread,
Sep 29, 2011, 10:55:04 AM9/29/11
to webd...@googlegroups.com
I'm doing this

((IJavaScriptExecutor)webDriver).ExecuteScript("if (window.screen){window.moveTo(0, 0);window.resizeTo(window.screen.availWidth,window.screen.availHeight);};");


Jason Leyba

unread,
Sep 29, 2011, 12:45:18 PM9/29/11
to webd...@googlegroups.com
On Thu, Sep 29, 2011 at 7:55 AM, Moises Siles <moises...@gmail.com> wrote:
I'm doing this

((IJavaScriptExecutor)webDriver).ExecuteScript("if (window.screen){window.moveTo(0, 0);window.resizeTo(window.screen.availWidth,window.screen.availHeight);};");

^ Yup, this is the way to do it.  Just a heads up though, the JavaScript functions window.moveTo and winodw.resizeTo have no effect in Chrome.  If you want to maximize a chrome browser, you'll have to start the browser maximized using DesiredCapabilities.  In Java:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));
WebDriver driver = new ChromeDriver(capabilities); 

-- Jason

Moises Siles

unread,
Sep 29, 2011, 12:51:52 PM9/29/11
to webd...@googlegroups.com
Thanks Jason, I didn't know that

Kris

unread,
Sep 29, 2011, 3:41:01 PM9/29/11
to webdriver
If you want it to be truly maximized (i.e. not merely resized to the
screen height/width), here's a little bit of black magic that I came
up with:

[Test]
public void Maximize(int browser)
{
IWebDriver driver = GetDriver(browser);

string script;
string name;

/* Some black magic to maximize Chrome. --Kris */
if (browser == Globals.CHROME)
{
name = "ed47cd2a4fcb5534a49f6eeb3bfcc564 - Google
Search";

driver.Navigate().GoToUrl("http://www.google.com/
search?q=ed47cd2a4fcb5534a49f6eeb3bfcc564");
}
else
{
/* Just in case the black magic below doesn't
work.... --Kris */
script = "window.moveTo( 0, 1 ); ";
script += "window.resizeTo( screen.width,
screen.height );";

((IJavaScriptExecutor)driver).ExecuteScript(script);

name = "ed47cd2a4fcb5534a49f6eeb3bfcc564";

script = "document.title='" + name + "';";

((IJavaScriptExecutor)driver).ExecuteScript(script);
}

/* This is some real voodoo magic here! Muaa ha ha ha!!
--Kris */
IntPtr hWnd = FindWindow(null, name + " - " +
Globals.BrowserPIDName(browser));
if (!hWnd.Equals(IntPtr.Zero))
{
ShowWindowAsync(hWnd, 3); // 3 = Maximize! --Kris
}
}

[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int
nCmdShow);

[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string sClassName,
string sAppName);


You can probably clean out the JS stuff. Also, I wasn't aware of the
--start-maximized Chrome switch when I wrote this, so I would replace
the Chrome block to what Jason suggested. =)

--Kris


On Sep 29, 9:51 am, Moises Siles <moises.si...@gmail.com> wrote:
> Thanks Jason, I didn't know that
>
> On Thu, Sep 29, 2011 at 10:45 AM, Jason Leyba <jmle...@gmail.com> wrote:
> > On Thu, Sep 29, 2011 at 7:55 AM, Moises Siles <moises.si...@gmail.com>wrote:
>
> >> I'm doing this
>
> >> ((IJavaScriptExecutor)webDriver).ExecuteScript("if
> >> (window.screen){window.moveTo(0,
> >> 0);window.resizeTo(window.screen.availWidth,window.screen.availHeight);};");
>
> > ^ Yup, this is the way to do it.  Just a heads up though, the JavaScript
> > functions window.moveTo and winodw.resizeTo have no effect in Chrome.  If
> > you want to maximize a chrome browser, you'll have to start the browser
> > maximized using DesiredCapabilities.  In Java:
>
> > DesiredCapabilities capabilities = DesiredCapabilities.chrome();
> > capabilities.setCapability("chrome.switches",
> > Arrays.asList("--start-maximized"));
> > WebDriver driver = new ChromeDriver(capabilities);
>
> > -- Jason
>

Maddog

unread,
Oct 2, 2011, 5:44:53 AM10/2/11
to webdriver
Great piece of code Kris - that's exactly what I thought of doing to
find the process id of the browser, setting unique title and finding
the window, but I think you can do the same with about:blank (push
document.title), why go to google?

Thanks for the code sample

TCBlues

unread,
Oct 5, 2011, 12:43:21 PM10/5/11
to webdriver
Thanks for the help,
I could manage how to do it on Chrome:

driver=Selenium::WebDriver.for(:chrome, :switches => %w[ --start-
maximized])

still looking for the same in other browsers since the javascript
method it is not the nicest solution ;)

Kris

unread,
Oct 5, 2011, 3:21:59 PM10/5/11
to webdriver
Heh good point, Maddog! I hadn't even thought of that.

--Kris

Kris

unread,
Oct 5, 2011, 3:23:34 PM10/5/11
to webdriver
Oh and super bonus points if you can un-hash the md5 string in the
search query lol. =)

--Kris

Simon Stewart

unread,
Nov 14, 2011, 7:28:18 AM11/14/11
to webd...@googlegroups.com
We're working on an experimental window controlling API. This hasn't
been implemented for Chrome yet, but is in Firefox:

driver.manage().window()

Is your way in.

Simon

Aravinth Bheemaraj

unread,
Feb 16, 2012, 3:09:19 PM2/16/12
to webd...@googlegroups.com
@Simon : Just curious to know if the window controlling API is up for Chrome?

shen xieyin

unread,
Feb 16, 2012, 10:45:39 PM2/16/12
to webd...@googlegroups.com
Guys, I tried the following code from other post, but it does not work FF9, is it only applied for FF7? Any idea for my case?

   Set<String> handles = driver.getWindowHandles(); 
            String script = "if (window.screen){window.moveTo(0, 0);window.resizeTo(window.screen.availWidth,window.screen.availHeight);};";
    
            ((JavascriptExecutor) driver).executeScript(script); 
            Set<String> newHandles = driver.getWindowHandles(); 
            newHandles.removeAll(handles); 
            driver.switchTo().window(newHandles.iterator().next()); 


-Shen

2012/2/17 Aravinth Bheemaraj <arav...@google.com>
@Simon : Just curious to know if the window controlling API is up for Chrome?

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/DfNYMLVCG-kJ.

Krishnan Mahadevan

unread,
Feb 17, 2012, 1:05:12 AM2/17/12
to webd...@googlegroups.com
Latest versions of FF dont let you maximize the window via Javascript. The only exception is, when a window itself is being spawned via Javascript.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

Jakub Siberski

unread,
Feb 17, 2012, 3:57:57 AM2/17/12
to webd...@googlegroups.com
As it was already discussed, due to reason Krishnan is mentioning, the only workaround I found that works is:

//create new driver instance, then

        //so let's use this trick
        final JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.open('','testwindow','width=400,height=200')");
        driver.close();
        driver.switchTo().window("testwindow");
        js.executeScript("window.moveTo(0,0);");
        js.executeScript("window.resizeTo(1280,800);");

//then just use driver instance

just replace dimensions with screen size
--
Pozdrawiam
Jakub Siberski

shen xieyin

unread,
Feb 19, 2012, 9:02:43 PM2/19/12
to webd...@googlegroups.com
Thanks Jakub, this way works fine. Just wonder why here open the windown "testwindow" and then close the driver? if it's closed, why can we still switch to it?

Richard Lavoie

unread,
Feb 19, 2012, 9:16:09 PM2/19/12
to webd...@googlegroups.com
If I'm nor wrong, what you close isn't the driver but the current window.  When you close the last window the webdriver behavior is to quit the browser.

That's why you can switch to it : you create a new window and close the current, hence there is still a window open, you can switch to it and the browser is not closed.

Richard

Jakub Siberski

unread,
Feb 20, 2012, 4:41:34 AM2/20/12
to webd...@googlegroups.com
You could just switch to new window, and leave original window open. But just in case I prefer to get rid of obsolete window.
Maybe you could close differently old window, but I use driver specifics (as Richard explained).

Patitapaban Mahapatra

unread,
Oct 26, 2013, 7:10:56 AM10/26/13
to webd...@googlegroups.com
directly u can use:: driver.manage().window().maximize();
Reply all
Reply to author
Forward
0 new messages