How to Minimize Browser Window in Selenium WebDriver
To minimize the browser window in Selenium WebDriver, use the
driver.manage().window().minimize() method. This command reduces the browser window to the taskbar or dock, allowing tests to continue running in the background.Syntax
The syntax to minimize the browser window in Selenium WebDriver is:
driver: Your WebDriver instance controlling the browser.manage(): Accesses browser management options.window(): Controls the browser window.minimize(): Minimizes the browser window.
java
driver.manage().window().minimize();
Example
This example shows how to open a Chrome browser, navigate to a website, minimize the window, and then close the browser.
java
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class MinimizeWindowExample { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); driver.get("https://www.example.com"); // Minimize the browser window driver.manage().window().minimize(); // Wait to see the minimized state Thread.sleep(3000); driver.quit(); } }
Output
Browser opens example.com, then minimizes the window for 3 seconds, then closes.
Common Pitfalls
Common mistakes when minimizing the browser window include:
- Using
driver.manage().window().minimize()in older Selenium versions that do not support it (introduced in Selenium 4). - Confusing
minimize()withmaximize()orfullscreen(). - Not waiting after minimizing, which may cause tests to fail if elements are not interactable.
Always ensure your Selenium version is 4 or higher to use minimize().
java
/* Wrong way - using maximize instead of minimize */ driver.manage().window().maximize(); // This maximizes, not minimizes /* Right way */ driver.manage().window().minimize();
Quick Reference
| Action | Selenium Command |
|---|---|
| Minimize window | driver.manage().window().minimize(); |
| Maximize window | driver.manage().window().maximize(); |
| Fullscreen window | driver.manage().window().fullscreen(); |
Key Takeaways
Use driver.manage().window().minimize() to minimize the browser window in Selenium 4+.
Ensure your Selenium WebDriver version supports the minimize() method.
Minimizing the window does not close the browser; tests continue running.
Do not confuse minimize() with maximize() or fullscreen() methods.
Add waits if your test depends on window state changes after minimizing.