Selenium – Check if window is minimized

Python Selenium – Check if the browser window is Minimized

To check if given browser window is minimized in Selenium, you can check if the document is hidden, by executing the JavaScript code that returns the hidden attribute value of the document.

Call execute_script() function on the driver object, and pass the JavaScript code that returns the document’s hidden attribute value.

driver.execute_script("return document.hidden;")

The above expression returns True if the document is hidden (minimized), or False if the document is not hidden.

Example

In the following program, we shall initialize a Chrome driver, load a URL, and check if the browser window is in minimized 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')

# Check if window is minimized
is_minimized = driver.execute_script("return document.hidden;")
if is_minimized:
    print('Window is in minimized state.')
else:
    print('Window is not in minimized state.')

# Close the driver
driver.quit()

Output

Window is not in minimized state.

Let us minimize the window after initializing the driver instance, and then check if the window is in minimized 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.minimize_window()

# Check if window is minimized
is_minimized = driver.execute_script("return document.hidden;")
print(is_minimized)
if is_minimized:
    print('Window is in minimized state.')
else:
    print('Window is not in minimized state.')

# Close the driver
driver.quit()

Output

Window is in minimized state.

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

Summary

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

Related Tutorials

Quiz on Selenium

Q1. What is the method used to interact with web elements in Selenium Python?

Not answered

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

Not answered

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

Not answered

Q4. Which of the following is not a commonly used web element locator 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 👍