0
0
Selenium-pythonHow-ToBeginner ยท 3 min read

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() with maximize() or fullscreen().
  • 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

ActionSelenium Command
Minimize windowdriver.manage().window().minimize();
Maximize windowdriver.manage().window().maximize();
Fullscreen windowdriver.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.