How to minimize webpage in Selenium Python

53 Views Asked by At

How can I minimize the browser window using selenium in python language? I use Chrome.

--Headless and driver.minimize_window() doesn't work for me. When I use driver.minimize_window(), it minimizes the browser but my code won't go any further. I used browser.set_window_position(-900, 950) and it's working fine, but I'd like to be able to minimize, not move and hide the browser.

2

There are 2 best solutions below

2
Mahboob Nur On

Install the package keyboard. And Try like this

from selenium import webdriver
import keyboard

# Set up Chrome options
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--start-maximized")  # Optional: Ensure the window is maximized initially
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option("useAutomationExtension", False)

# Create a Chrome driver
driver = webdriver.Chrome(options=chrome_options)

# Navigate to a URL (replace with your URL)
driver.get("https://www.example.com")

# Minimize the browser window using keyboard shortcut
keyboard.press_and_release("win+down")  # Minimize the window on Windows

# Add a delay to give time for the window to minimize
driver.implicitly_wait(2)

# Perform other actions or navigate further
# ...

# Close the browser
driver.quit()
1
Infern0 On

i would suggest to use the official API doc from selenium. There is command for such operation. https://www.selenium.dev/documentation/webdriver/interactions/windows/#minimize-window

Select the specific language from tabs, in your case should be:

driver.minimize_window()