How to open browser window as minimized in selenium python?

Viewed 588

I have a script that converts html to pdf using selenium(Have tried all other methods of converting it but the html file is a bit complicated. So no other methods are working). It's working perfectly. But the problem is that it opens a new window every time I want to print the page. I want it to be opened minimized. If anyone knows how to do it, I would appreciate it.(printing pdf in headless mode doesn't works)

My code:

from selenium import webdriver

options = webdriver.ChromeOptions()
settings = {
    "recentDestinations": [{
        "id": "Save as PDF",
        "origin": "local",
        "account": ""
    }],
    "selectedDestinationId": "Save as PDF",
    "version": 2,
    "isHeaderFooterEnabled": False,
    "isCssBackgroundEnabled": True
}
prefs = {
    'printing.print_preview_sticky_settings.appState': json.dumps(settings),
    'savefile.default_directory': "./"
}
options.add_experimental_option('prefs', prefs)
options.add_argument('--kiosk-printing')
options.add_argument('--enable-print-browser')
options.add_argument("--disable-popup-blocking")
options.add_argument('--disable-extensions')
driver = webdriver.Chrome('chromedriver', options=options)
full_path = "file:///home/zubayer/test.html"
driver.get(full_path)
time.sleep(2)
driver.execute_script('window.print();')
driver.quit()

Example html file:

<!DOCTYPE html>
<html>
</head>
    <title>Test</test>
</head>
<body>
    <h1>This is a test</h1>
    <p>Help me</p>
</body>
</html>
1 Answers

There is a method to minimize window. Add this line to your code.

driver.minimize_window()
Related