6

After maximizing the window by driver.manage().window().maximize();, how do I minimize the browser window in Selenium WebDriver with Java?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Armaan
  • 61
  • 1
  • 1
  • 3
  • 1
    Possible duplicate of [How could I start a Selenium browser(like Firefox) minimized?](http://stackoverflow.com/questions/6643951/how-could-i-start-a-selenium-browserlike-firefox-minimized) – Chanda Korat Mar 07 '17 at 12:21

9 Answers9

5

Selenium's Java client doesn't have a built-in method to minimize the browsing context.

However, as the default/common practice is to open the browser in maximized mode, while Test Execution is in progress minimizing the browser would be against the best practices as Selenium may lose the focus over the browsing context and an exception may raise during the test execution. However, Selenium's Python client does have a minimize_window() method which eventually pushes the Chrome browsing context effectively to the background.


Sample code

Python:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.google.co.in')
driver.minimize_window()

Java:

driver.navigate().to("https://www.google.com/");
Point p = driver.manage().window().getPosition();
Dimension d = driver.manage().window().getSize();
driver.manage().window().setPosition(new Point((d.getHeight()-p.getX()), (d.getWidth()-p.getY())));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
4

There seems to be a minimize function now:

From the documentation on:

webdriver.manage().window()

this.minimize() → Promise<undefined>
Minimizes the current window. The exact behavior of this command is specific to individual window managers, but typicallly involves hiding the window in the system tray.

Parameters
None.

Returns
Promise<undefined>
A promise that will be resolved when the command has completed.

So the code should be:

webdriver.manage().window().minimize()

At least in JavaScript.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
chitzui
  • 3,778
  • 4
  • 28
  • 38
  • Thanks for including the "JavaScript" disclaimer. https://www.selenium.dev/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_IWindow.htm says the Minimize function does not exist for .NET Framework developers. – Zian Choy Nov 20 '21 at 00:14
2

Unfortunately, Selenium does not provide any built-in function for minimizing the browser window. There is only the function for maximizing the window. But there is some workaround for doing this.

driver.manage().window().setPosition(new Point(-2000, 0));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Govind Raj
  • 105
  • 6
  • As mentioned in [DebanjanB's answer](https://stackoverflow.com/questions/42647058/how-to-minimize-the-browser-window-in-selenium-webdriver-3/60379177#60379177), the minimize_window() function works fine from Python. I have used it for Firefox. – Peter Mortensen Nov 22 '20 at 00:17
0

Use the following code to the minimize browser window. It worked for me and I am using Selenium 3.5:

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_N);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rishab
  • 11
  • 2
0

I'm using Selenium WebDriver (Java) 3.4.0 and have not found the minimize function either. Then I'm using the following code.

driver.manage().window().maximize();
driver.manage().window().setPosition(new Point(0, -2000));

We need to import the following for the Point function.

import org.openqa.selenium.Point;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
KHACHORNCHIT
  • 2,222
  • 23
  • 19
0

For C# , tested using Selinium 3.14 ver ( Package1 , Package2) and it work

    IWebDriver driver = new ChromeDriver();
    driver.Manage().Window.Minimize();
HO LI Pin
  • 1,441
  • 13
  • 13
0

In Selenium version 4, there is a method called minimize() added in Window interface (An inner interface of WebDriver) with Java. Hence now, if you have Selenium version 4, then you can use driver.manage().window().minimize().

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
0

This works for me in Python.

The window will open and after loading it will minimize.

driver.get(url)
driver.minimize_window()
isopach
  • 1,783
  • 7
  • 31
  • 43
-1

Try this. It should work.

Dimension n = new Dimension(360, 592);  
driver.manage().window().setSize(n);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131