Selenium – Check if browser window is Maximized

Python Selenium – Check if the browser window is Maximized

To check if given browser window is maximized in Selenium, get the initial size of the window, then maximize the window and get the size of the maximized window. Compare these two window sizes. If they are same, then the window is in maximized state.

You may set the browser size back to the initial size, after checking the browser size.

Example

In the following program, we shall implement the above said solution, and check if the browser window is in maximized state.

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

# Initialize chrome driver instance
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index.html')

# Get initial window size
initial_window_size = driver.get_window_size()
print('Initial window size :' + str(initial_window_size))

# Get initial window size
initial_window_size = driver.get_window_size()
print('Initial window size :' + str(initial_window_size))

# Get maximized window size
driver.maximize_window()
maximized_window_size = driver.get_window_size()
print('Maximized window size :' + str(maximized_window_size))

# Reset window size to its initial size
driver.set_window_size(initial_window_size['width'], initial_window_size['height'])

# Check if window is maximized
if initial_window_size == maximized_window_size:
    print('Window is in maximized state.')
else:
    print('Window is not in maximized state.')

# Close the driver
driver.quit()

Output

Initial window size :{'width': 800, 'height': 600}
Maximized window size :{'width': 1080, 'height': 1895}
Window is not in maximized state.

Let us maximize the window after initializing the driver instance, and then check if the window is in maximized state.

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

# Initialize chrome driver instance
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.maximize_window()

# Get initial window size
initial_window_size = driver.get_window_size()
print('Initial window size :' + str(initial_window_size))

# Get maximized window size
driver.maximize_window()
maximized_window_size = driver.get_window_size()
print('Maximized window size :' + str(maximized_window_size))

# Reset window size to its initial size
driver.set_window_size(initial_window_size['width'], initial_window_size['height'])

# Check if window is maximized
if initial_window_size == maximized_window_size:
    print('Window is in maximized state.')
else:
    print('Window is not in maximized state.')

# Close the driver
driver.quit()

Output

Initial window size :{'width': 1080, 'height': 1895}
Maximized window size :{'width': 1080, 'height': 1895}
Window is in maximized state.

Since the window is already maximized before checking, we got the result that the window is in maximized state.

Summary

In this Python Selenium Tutorial, we have seen how to check if given browser window is maximized or not.

Related Tutorials

Quiz on Selenium

Q1. What is a web driver in Selenium Python?

Not answered

Q2. What is the method used to find web elements in Selenium Python?

Not answered

Q3. Which of the following is not a method to wait for a web element to load in Selenium Python?

Not answered

Q4. Which of the following is not a commonly used assertion method in Selenium Python?

Not answered

Q5. What is the method used to select a value from a drop-down list in Selenium Python?

Not answered
Code copied to clipboard successfully 👍