【发布时间】:2019-11-15 17:35:17
【问题描述】:
我已经尝试了所有可以在 Internet 上找到的解决方案,以便能够在 Python 中打印在 Selenium 中打开的页面。但是,当打印弹出窗口出现时,一两秒后它消失了,没有保存任何 PDF。
这是正在尝试的代码。基于这里的代码 - https://stackoverflow.com/a/43752129/3973491
使用 Mojave 10.14.5 在 Mac 上编码。
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import WebDriverException
import time
import json
options = Options()
appState = {
"recentDestinations": [
{
"id": "Save as PDF",
"origin": "local"
}
],
"selectedDestinationId": "Save as PDF",
"version": 2
}
profile = {'printing.print_preview_sticky_settings.appState': json.dumps(appState)}
# profile = {'printing.print_preview_sticky_settings.appState':json.dumps(appState),'savefile.default_directory':downloadPath}
options.add_experimental_option('prefs', profile)
options.add_argument('--kiosk-printing')
CHROMEDRIVER_PATH = '/usr/local/bin/chromedriver'
driver = webdriver.Chrome(options=options, executable_path=CHROMEDRIVER_PATH)
driver.implicitly_wait(5)
driver.get(url)
driver.execute_script('window.print();')
$chromedriver --v
ChromeDriver 75.0.3770.90 (a6dcaf7e3ec6f70a194cc25e8149475c6590e025-refs/branch-heads/3770@{#1003})
关于如何将打开的 html 页面打印为 PDF 的任何提示或解决方案。花了几个小时试图完成这项工作。谢谢!
2019-07-11 更新:
我的问题已被确定为重复,但 a) 另一个问题似乎是使用 javascript 代码,并且 b) 答案并没有解决这个问题中提出的问题 - 这可能与更新的软件有关版本。正在使用的 Chrome 版本是版本 75.0.3770.100 (Official Build) (64-bit),chromedriver 是 ChromeDriver 75.0.3770.90。在 Mac OS Mojave 上。脚本在 Python 3.7.3 上运行。
2019-07-11 更新:
把代码改成
from selenium import webdriver
import json
chrome_options = webdriver.ChromeOptions()
settings = {
"appState": {
"recentDestinations": [{
"id": "Save as PDF",
"origin": "local",
"account": "",
}],
"selectedDestinationId": "Save as PDF",
"version": 2
}
}
prefs = {'printing.print_preview_sticky_settings': json.dumps(settings)}
chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument('--kiosk-printing')
CHROMEDRIVER_PATH = '/usr/local/bin/chromedriver'
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=CHROMEDRIVER_PATH)
driver.get("https://google.com")
driver.execute_script('window.print();')
driver.quit()
而现在,什么也没有发生。 Chrome 启动,加载 url,出现打印对话框,但似乎什么都没有发生 - 默认打印机队列中没有任何内容,也没有 pdf - 我什至通过在 Mac 上查找“最近的文件”来搜索 PDF 文件。
【问题讨论】:
-
没有保存 PDF,你在哪里查的?它应该保存在您的用户下载文件夹中。
-
@Kamal - 我又试了一次,发现 Chrome 在我的默认打印机上触发了一个实际的打印输出,但我不在同一个位置,所以我没有注意到实际发生了什么。从我尝试打印到 pdf/ 的无数次中删除了打印队列,似乎什么也没发生。所以我怀疑“另存为 PDF”选项没有被选中并且不知道如何选择它。
-
请参考这个answer。在您的代码中,您调用的是
webdriver.Chrome(options=options..,但正确的语法是webdriver.Chrome(chrome_options=options..。不知何故,webdriver.ChromeOptionsprint 的工作速度比webdriver.chrome.options.Options快,所以我建议你尝试一下。 -
@GregW.F.R 很高兴它成功了。我已经很久没有使用这个了。但是是的,这就是实例化 chrome 驱动程序实例的方式。
标签: python python-3.x selenium selenium-webdriver selenium-chromedriver