如何在Selenium(Python)中将打开的页面保存为pdf [英] how to save opened page as pdf in Selenium (Python)

查看:443
本文介绍了如何在Selenium(Python)中将打开的页面保存为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试了所有我能在 Internet 上找到的解决方案,以便能够打印在 Python 中在 Selenium 中打开的页面.然而,当打印弹出窗口出现时,一两秒钟后它就会消失,没有保存 PDF.

Have tried all the solutions I could find on the Internet to be able to print a page that is open in Selenium in Python. However, while the print pop-up shows up, after a second or two it goes away, with no PDF saved.

这是正在尝试的代码.基于此处的代码 - https://stackoverflow.com/a/43752129/3973491

Here is the code being tried. Based on the code here - https://stackoverflow.com/a/43752129/3973491

使用 Mojave 10.14.5 在 Mac 上编码.

Coding on a Mac with Mojave 10.14.5.

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 的任何提示或解决方案.花了几个小时试图使这项工作.谢谢!

Any hints or solutions as to what can be done to print the open html page to a PDF. Have spent hours trying to make this work. Thank you!

2019-07-11 更新:

Update on 2019-07-11:

我的问题已被识别为重复,但是 a) 另一个问题似乎使用了 javascript 代码,并且 b) 答案没有解决此问题中提出的问题 - 可能与更新的软件有关版本.使用的 Chrome 版本是 75.0.3770.100(官方版本)(64 位),chromedriver 是 ChromeDriver 75.0.3770.90.在 Mac OS Mojave 上.脚本在 Python 3.7.3 上运行.

My question has been identified as a duplicate, but a) the other question seems to be using javascript code, and b) the answer does not solve the problem being raised in this question - it may be to do with more recent software versions. Chrome version being used is Version 75.0.3770.100 (Official Build) (64-bit), and chromedriver is ChromeDriver 75.0.3770.90. On Mac OS Mojave. Script is running on Python 3.7.3.

2019-07-11 更新:

Update on 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 文件.

And now, nothing happens. Chrome launches, loads url, print dialog appears but then nothing seems to happen - nothing in the default printer queue, and no pdf either - I even searched for the PDF files by looking up "Recent Files" on Mac.

推荐答案

答案 此处,当我的操作系统中没有任何其他打印机设置时工作.但是当我有另一台默认打印机时,这不起作用.

The answer here, worked when I did not have any other printer setup in my OS. But when I had another default printer, this did not work.

我不明白怎么做,但以这种方式做一些小的改变似乎是可行的.

I don't understand how, but making small change this way seems to work.

from selenium import webdriver
import json

chrome_options = webdriver.ChromeOptions()
settings = {
       "recentDestinations": [{
            "id": "Save as PDF",
            "origin": "local",
            "account": "",
        }],
        "selectedDestinationId": "Save as PDF",
        "version": 2
    }
prefs = {'printing.print_preview_sticky_settings.appState': 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()

这篇关于如何在Selenium(Python)中将打开的页面保存为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆