Can arouse the pop-up in the script of the printed page? Or don't open this function calls in the script and the printed page also can
The main reason is needed is required to format PDF file content, request and the browser CTRL + p to print the exported PDF format is the same, so can't use the selenium method take screenshots of web pages,
CodePudding user response:
Don't know if the selenium can arouse the printed page directly, but you can at the time of need to print automatically enter CTRL + p shortcuts,#! The/usr/bin/env python3
# - * - coding: utf-8 - * -
The import time
The import pyautogui
The from the selenium import webdriver
Re=webdriver. Chrome ()
Re. The get (' https://www.baidu.com ')
Pyautogui. Hotkey (' CTRL ', 'p')
Time. Sleep (2)
Re. The quit ()
CodePudding user response:
Method 1, the use of Wkhtmltopdf kit can be directly put the HTML to PDFMethod 2, the use of selenium to save PDF
The import json
The from the selenium import webdriver
Driver=webdriver. Chrome ()
The resource="/session/% s/chromium/send_command_and_get_result" % driver. The session_id
Url=driver.com mand_executor. _url + resource
Body=json. Dumps ({' CMD ':' Page. PrintToPDF ', 'params: {
"Landscape" : False,
'displayHeaderFooter: False,
'printBackground: True,
'preferCSSPageSize: True,
}})
Response=driver.com mand_executor. _request (' POST 'url, body)
Result=response. Get (' value ')
With the open (' report. PDF ', 'wb) as file:
File. The write (result)