Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to minimize the browser window in Selenium WebDriver 3

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

like image 961
Armaan Avatar asked Mar 07 '17 11:03

Armaan


2 Answers

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.

like image 60
chitzui Avatar answered Sep 20 '22 18:09

chitzui


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())));
like image 20
undetected Selenium Avatar answered Sep 21 '22 18:09

undetected Selenium