如何在Python语言中使用selenium最小化浏览器窗口? 我使用 Chrome。
--Headless 和 driver.minimize_window() 对我不起作用。 当我使用 driver.minimize_window() 时,它会最小化浏览器,但我的代码不会再继续。 我使用 browser.set_window_position(-900, 950) 并且它工作正常,但我希望能够最小化,而不是移动和隐藏浏览器。
安装软件包键盘。并尝试这样
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()