提问人:Seth Kramer 提问时间:6/4/2023 最后编辑:Seth Kramer 更新时间:6/5/2023 访问量:156
为什么 Chrome Webdriver 在 Python 中使用 Selenium 打印时不保存文件?
Why isn't Chrome Webdriver saving files when printing with Selenium in Python?
问:
我正在使用 python 脚本和 selenium 打开各种带有 window.print() 的网页,以使用 kiosk 打印打印一系列页面。直到 2023 年 5 月 15 日,当我在 chrome 113 上时,一切都在工作,但在 114 上使用最新的网络驱动程序,对话框打开,它实际上并没有将文件保存在指定位置。
from bs4 import BeautifulSoup
import html2text
import time
import os
import glob
import json
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from datetime import date
from datetime import datetime
import pandas as pd
from selenium.webdriver.chrome.options import Options
PATH = "/usr/local/bin/chromedriver"
s = Service(PATH)
self._experimental_options = {}
def add_experimental_option(self, name, value):
"""
Adds an experimental option which is passed to chrome.
Args:
name: The experimental option name.
value: The option value.
"""
self._experimental_options[name] = value
print_settings = {
"recentDestinations": [{
"id": "Save as PDF",
"origin": "local",
"account": "",
}],
"selectedDestinationId": "Save as PDF",
"version": 2
}
prefs = {'printing.print_preview_sticky_settings.appState': json.dumps(print_settings),
"download.prompt_for_download": False,
"profile.default_content_setting_values.automatic_downloads": 1,
"download.default_directory": "/Users/seth/Downloads/pdfs",
"savefile.default_directory": "/Users/seth/Downloads/pdfs",
"download.directory_upgrade": True,
"safebrowsing.enabled": True}
options = Options()
options.add_experimental_option('prefs', prefs)
options.add_argument('--kiosk-printing')
driver = webdriver.Chrome(service=s, options=options)
然后我只需加载我的自打印页面
driver.get("https://www.heresmyprintingpage.com/1.htm")
打印对话框像正在打印一样加载,但它不会打印到我指定的目录或我可以知道的任何地方。我一辈子都无法弄清楚为什么当我成功使用它一年多时,它停止与最新版本一起工作。
我尝试更改默认目录字符串,验证该文件夹非常宽松。
答: 暂无答案
评论
Service
IA 未定义。 未定义(它通常建议使用类)。 没有定义,没有定义......据推测,这些来自导入的模块......请说明是否是。self
Options
webdriver