Maximize windows in selenium

How to get window size, resize or maximize window using Selenium WebDriver

Published: February 23, 2014 by Yi Zeng (Last updated: January 16, 2017 )

Recently Updated — January 16, 2017

Selenium WebDriver supports getting the browser window size, resizing and maximizing window natively from its API, no JavaScript injections like window.resizeTo(X, Y); are necessary any more. Below shows the examples on how to achieve this in Selenium WebDriver C#, Ruby and Python bindings.

In Ruby binding, window size can be retrieved from method driver.manage.window.size , which is a type of struct Selenium::WebDriver::Dimension defined here. To resize a window, one solution is to create a new Dimension object and assign it to property driver.manage.window.size . Alternatively, Ruby binding has provided a driver.manage.window.resize_to() method, which is equivalent to #size= , but accepts width and height arguments according to API here.

Environment Tested:
Mac OS Sierra, Ruby 2.3.1p112, Selenium 3.0.5, ChromDriver 2.26, GeckoDriver 0.13
Firefox 50.1, Chrome 55, PhantomJS 1.9.8

Example

Output

Similarly in C# binding, a browser window’s size can be found out using driver.Manage().Window.Size property. The same IWindow interface also defines method Maximize() for maximizing the window. Although this interface doesn’t provide a function to resize window directly like Ruby binding, it can be done by setting the Size property using System.Drawing.Size object 1 .

Environment Tested:
Windows 7, Selenium 2.39.0, Firefox 26.0

Example

Output

Python

Unlike C# and Ruby bindings, Python binding doesn’t offer properties to get/set window size, all get/set/maximize actions are available using methods defined in selenium.webdriver.remote.webdriver.

Environment Tested:
Window 7, Python 2.7, Selenium 2.40.0, Firefox 26.0

How to Maximize Chrome Window in Selenium Webdriver using Java

By Jash Unadkat, Technical Content Writer at BrowserStack — June 24, 2020

Читайте также:  Как узнать номер сети компьютера windows

When browser windows are maximized, it reduces the chances of Selenium scripts missing out on web elements they must interact with during automated tests. It is possible that certain elements may not be visible to or recognized by Selenium if the browser window is not in a maximized state.

Maximizing a browser window at first also provides better visibility to the QAs for the test cases being executed. Thus QAs must consider maximizing the browser window as a best practice.

As Chrome is the most widely used browser, this article will explore two simple ways to maximize a Chrome window in Selenium Webdriver using Java.

1. Use the maximize() method from WebDriver.Window Interface

The code snippet below implements four basic scenarios:

  1. Launching the Chrome browser
  2. Navigating to the desired URL
  3. Maximizing the Chrome Window
  4. Terminating the browser

Code:

Successful execution of the selenium script above will do the following: launch the Chrome browser, navigate to the BrowserStack website, maximize the Chrome Window, and wait for five seconds.

2. Use the ChromeOptions class

An alternate method that can maximize the Chrome window is to use the ChromeOptions class. This method informs the Chrome browser explicitly to launch in maximized mode.

Code:

Successful execution of the script above will do the following: launch the Chrome browser in maximized mode and navigate to the Browserstack website.

Note: In the first method, the operation is performed after launching the Chrome browser. In the second method, the browser is, by default launched in maximized mode.

Maximizing a browser window prior to the execution of test cases provides better visibility to the QA. Doing so also ensures that no elements are left unidentified when tests are being executed. Implementing either of the methods explained above will help QAs avoid test failure.

Maximize Browser in Selenium

In this tutorial, you will learn how to maximize, minimize or resize the browser using selenium Webdriver. Explained through different scenarios using maximize() method and dimensions for resizing the browser.

Why Maximize a Browser in Selenium Automation?

Elements on the web application may not be recognized by the selenium if the browser is not maximized and thereby making framework fail. Hence, Maximize the browser is very important part of selenium framework. It is good practice to maximize the browser while automating any web application. When the user executes the selenium framework or any script, browser may not be in the full screen state and you need to maximize the browser to view all the elements of the web application. It is good to maximize the browser at the start of the script, so that the script gets executed successfully without any error.

Читайте также:  Regpolicy что это за папка windows

How to Maximize the Browser in Selenium

To maximize a browser window, you need to call the maximize() method of the Window interface of the driver class.

You can customize the size of the browser according to the requirement of the scenario. Selenium webdriver does not provide any method for minimizing the browser, there is no such direct method. You need to use resize method to minimize the browser.

Example: Resize a browser using selenium web driver

a) Selenium script with explanation.

Script Description : In the below Selenium script shown the resize of the browser using testNG framework , steps of the scenario are :

  1. Open the chrome browser .
  2. Launch the site .
  3. Wait for a few seconds as to view the resize action .
  4. Close the browser.

b) Output Analysis

Opened the chrome browser , resized the browser, wait for a few seconds and closed the browser.

Example: Maximize a browser window using web driver.

a) Selenium script with explanation.

Script Description : In the below Selenium script shown the maximize of the browser using testNG framework , steps of the scenario are :

  1. Open the chrome browser .
  2. Launch the site .
  3. Wait for a few seconds as to view the maximize action .
  4. Close the browser.

b) Output Analysis

Opened the chrome browser , maximized the browser, wait for a few seconds and closed the browser.

Example: Minimize a browser window using web driver.

a) Selenium script with explanation.

Script Description : In the below Selenium script shown the minimize of the browser using testNG framework , steps of the scenario are :

  1. Open the chrome browser .
  2. Launch the site .
  3. Wait for a few seconds as to view the minimize action .
  4. Close the browser.

Note: If the user wants to use Firefox browser, then user needs to set property of FirefoxDriver and create FirefoxDriver object instead of ChromeDriver in all above 3 scenarios scripts as given below:

b) Output Analysis

Opened the chrome browser , minimized the browser, wait for a few seconds and closed the browser.

How to Maximize a firefox browser window using Selenium WebDriver with node.js

How do we maximize a firefox browser using Selenium WebDriver (Selenium 2) with node.js. I am using wd package for Selenium WebDriver to write the tests. I have tried executing window.resizeTo(1366,768); usin eval or execute but didn’t work.

I am using Selenium WebDriver 2.25.0

13 Answers 13

First instantiate a firefox driver

then maximize it

Please make sure that you give enough time for the window to load before you declare this statement.

Читайте также:  Как устанавливать windows 10 с флешки bios uefi

If you are finding any element to input some data then provide reasonable delay between this and the input statement.

Try this working for Firefox:

If you are using Selenium WebdriverJS than the below code should work:

Call window as a function.

The statement :: driver.manage().window.maximize(); Works perfectly, but the window maximizes only after loading the page on browser, it does not maximizes at the time of initializing the browser.

here : (driver) is called object of Firefox driver, it may be any thing depending on the initializing of Firefox object by you only.

One way, everyone already have written in thier answers:

but if you looking for alternative ways, here you are:

works perfectly and at the very beginning it maximizes the window. Does not wait to load any page.

As of 2020, the correct answer is :

driver.maximize_window()

Not the answer you’re looking for? Browse other questions tagged firefox selenium-webdriver or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

How to Maximize window in chrome using webDriver (python)

Is there a way to maximize the chrome browser window using python selenium WebDriver?

Note: I am using Chrome Driver 23.0 Any solution on this would be greatly appreciated!

9 Answers 9

You could use ChromeOptions and set suitable argument:

For MAC or Linux:

Nothing worked for me except:

I found this by inspecting selenium/webdriver/remote/webdriver.py. I’ve never found any useful documentation, but reading the code has been marginally effective.

Based on what Janek answered, this worked for me (Linux):

This works for me, with Mac OS Sierra using Python,

I do the following:

try this, tested on windows platform and it works fine :

Not the answer you’re looking for? Browse other questions tagged python selenium selenium-chromedriver or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Оцените статью