当前位置: 首页 > 知识库问答 >
问题:

如何在Selenium(Python)中将打开的页面保存为pdf格式

慕容明煦
2023-03-14

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

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

2019-07-11更新:

我的问题被认为是重复的,但是a)另一个问题似乎是使用javascript代码,b)答案没有解决这个问题中提出的问题--可能与最近的软件版本有关。使用的Chrome版本是version 75.0.3770.100(官方版本)(64位),chromedriver是chromedriver 75.0.3770.90。在Mac OS上Mojave.Script在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文件。

共有1个答案

鄂琛
2023-03-14

当我的操作系统中没有任何其他打印机设置时,这里的答案是有效的。但当我有另一台默认打印机时,这就不起作用了。

我不明白怎么做,但用这种方式做点零钱似乎很管用。

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()
 类似资料: