繁体   English   中英

如何在 selenium python 中最小化打开浏览器 window?

[英]How to open browser window as minimized in selenium python?

我有一个使用硒将 html 转换为 pdf 的脚本(已经尝试了所有其他转换它的方法,但是 html 文件有点复杂。所以没有其他方法53EZ 文件有点复杂)。 它运行良好。 但问题是每次我想打印页面时它都会打开一个新的 window 。 我希望它以最小化的方式打开。 如果有人知道该怎么做,我将不胜感激。(在无头模式下打印 pdf 不起作用)

我的代码:

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()

示例 html 文件:

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

有一种方法可以最小化 window。 将此行添加到您的代码中。

driver.minimize_window()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM