【问题标题】:How to open browser window as minimized in selenium python?如何在 selenium python 中打开最小化的浏览器窗口?
【发布时间】:2021-04-10 13:16:32
【问题描述】:

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

【问题讨论】:

标签: python python-3.x selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

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

driver.minimize_window()

【讨论】:

  • 感谢您的回答。但是仔细看我的问题。我说过如何“打开”在 selenium 中最小化的浏览器。您提供的代码将首先打开浏览器,然后将其最小化。但我不会以最小化的方式打开它。
猜你喜欢
  • 2017-07-27
  • 2021-03-03
  • 2020-05-27
  • 1970-01-01
  • 1970-01-01
  • 2018-08-09
  • 2014-09-04
  • 1970-01-01
  • 2016-03-07
相关资源
最近更新 更多