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('/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.