0
0
Selenium Pythontesting~5 mins

Maximize and minimize window in Selenium Python

Choose your learning style9 modes available
Introduction

Maximizing or minimizing the browser window helps test how a website looks and works in different window sizes.

When you want to see the website in full screen to check layout issues.
When testing responsive design by resizing the browser window.
When you want to minimize distractions by hiding the browser during tests.
When automating tests that depend on window size or position.
When running tests on different screen resolutions.
Syntax
Selenium Python
driver.maximize_window()
driver.minimize_window()

These commands control the browser window size during tests.

Use them after opening the browser and before interacting with the page.

Examples
This opens Chrome and maximizes the window.
Selenium Python
from selenium import webdriver

driver = webdriver.Chrome()
driver.maximize_window()
This opens Chrome and minimizes the window.
Selenium Python
from selenium import webdriver

driver = webdriver.Chrome()
driver.minimize_window()
This maximizes the window, runs tests, then minimizes it.
Selenium Python
from selenium import webdriver

driver = webdriver.Chrome()
driver.maximize_window()
# do some testing

driver.minimize_window()
Sample Program

This script opens Chrome, maximizes the window, waits 2 seconds, minimizes the window, waits 2 seconds, then closes the browser. The print statements show the steps in the console.

Selenium Python
from selenium import webdriver
import time

# Open Chrome browser
driver = webdriver.Chrome()

# Maximize the browser window
driver.maximize_window()
print("Window maximized")

# Wait 2 seconds to see the effect
time.sleep(2)

# Minimize the browser window
driver.minimize_window()
print("Window minimized")

# Wait 2 seconds to see the effect
time.sleep(2)

# Close the browser
driver.quit()
OutputSuccess
Important Notes

Make sure the browser driver (like chromedriver) is installed and in your system path.

Some browsers or OS may not support minimize_window perfectly.

Use time.sleep() only for demonstration; in real tests, use explicit waits.

Summary

Use maximize_window() to make the browser full screen.

Use minimize_window() to hide the browser window.

These help test how your website behaves in different window sizes.