How to minimize the browser window in Selenium WebDriver 3
Asked Answered
A

9

6

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

Aquileia answered 7/3, 2017 at 11:24 Comment(1)
Possible duplicate of How could I start a Selenium browser(like Firefox) minimized?Trudi
C
6

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())));
Chiropractic answered 24/2, 2020 at 15:32 Comment(0)
I
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.

Ilo answered 18/10, 2018 at 15:2 Comment(1)
Thanks for including the "JavaScript" disclaimer. selenium.dev/selenium/docs/api/dotnet/html/… says the Minimize function does not exist for .NET Framework developers.Salisbury
C
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));
Coligny answered 7/3, 2017 at 11:29 Comment(1)
As mentioned in DebanjanB's answer, the minimize_window() function works fine from Python. I have used it for Firefox.Costume
H
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);
Hieroglyphic answered 4/10, 2017 at 9:20 Comment(0)
L
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;
Lefthander answered 12/4, 2018 at 16:6 Comment(1)
What is it supposed to do? What is the idea? Move the window off screen?Costume
R
0

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

    IWebDriver driver = new ChromeDriver();
    driver.Manage().Window.Minimize();
Rayfordrayle answered 20/1, 2022 at 7:24 Comment(0)
B
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().

Bedwell answered 20/3, 2022 at 5:24 Comment(0)
N
0

This works for me in Python.

The window will open and after loading it will minimize.

driver.get(url)
driver.minimize_window()
Noni answered 2/12, 2022 at 8:24 Comment(0)
S
-1

Try this. It should work.

Dimension n = new Dimension(360, 592);  
driver.manage().window().setSize(n);
Shelli answered 28/9, 2020 at 5:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.