当我执行ctrl p时,它为我提供了一个选择文件位置、打印属性等的窗口。我如何使用Selenium转到网页,使打印属性为彩色PDF,并将其保存在与Python文件所在的目录相同的目录中?谢谢你的帮助!
~你好世界
有两种方法:
- 使用无头模式并使用
Page.printToPDF
:
import json
from base64 import b64decode
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from datetime import datetime
options = webdriver.ChromeOptions()
options.add_argument("--headless")
driver = webdriver.Chrome(options=options)
driver.get("https://emicalculator.net/")
a = driver.find_element_by_css_selector("#loanamountslider")
webdriver.ActionChains(driver).click(
a).click_and_hold().move_by_offset(0, 0).perform()
a = driver.execute_cdp_cmd(
"Page.printToPDF", {"path": 'html-page.pdf', "format": 'A4'})
print(a)
# Import only b64decode function from the base64 module
# Define the Base64 string of the PDF file
b64 = a['data']
# Decode the Base64 string, making sure that it contains only valid characters
bytes = b64decode(b64, validate=True)
# Perform a basic validation to make sure that the result is a valid PDF file
# Be aware! The magic number (file signature) is not 100% reliable solution to validate PDF files
# Moreover, if you get Base64 from an untrusted source, you must sanitize the PDF contents
if bytes[0:4] != b'%PDF':
raise ValueError('Missing the PDF file signature')
# Write the PDF contents to a local file
f = open('file.pdf', 'wb')
f.write(bytes)
f.close()
- 对于非无头调用
window.print()
:
options = webdriver.ChromeOptions()
options.headless = False
options.add_argument("--kiosk-printing")
options.add_argument("--kiosk")
settings = {
"recentDestinations": [{
"id": "Save as PDF",
"origin": "local",
"account": "",
# This is the folder where i want to place my PDF (in the same directory as this
'default_directory': r'C:UserspraveDownloadstravelBAfolder'
# file)
}],
"selectedDestinationId": "Save as PDF",
"version": 2,
}
prefs = {
'printing.print_preview_sticky_settings.appState': json.dumps(settings),
#"savefile.default_directory": "C:UserspraveDownloadstravelBAfolder",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"download.safebrowsing.enabled": True
}
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(executable_path="chromedriver.exe", options=options)
driver.get("https://google.com")
# This gets saved in my downloads folder
driver.execute_script("window.print();")
input()