[英]Selenium printing not working (Print to PDF)
我想要硒:打開一個頁面,休眠 10 秒(確保所有內容都已加載),然后打印網頁。 使用 javascript 打印頁面或使用send_keys
兩種解決方案均send_keys
。
profile = FirefoxProfile(PATH_TO_PROFILE_HERE)
profile.set_preference("print.always_print_silent", True)
profile.set_preference("print.printer_Microsoft_Print_to_PDF.print_to_file", True)
profile.set_preference("print.printer_Microsoft_Print_to_PDF.print_to_filename", file_location)
driver = webdriver.Firefox(profile)
driver.get(URL_HERE)
time.sleep(10)
# Neither one of these two seem to work.
# driver.execute_script('window.print();')
# driver.find_element_by_css_selector("body").send_keys(Keys.CONTROL, "p")
當我手動進入問題頁面(使用配置文件並適當設置首選項)時,我可以使用Ctrl+P
並且打印時沒有對話框顯示在哪里打印/命名它。
問題是間歇性的,它可以在www.google.com
但不能在其他一些網站上運行? 有沒有人找到讓打印 100% 正常工作的方法?
你可以嘗試使用這個來代替使用硒:
https://pypi.org/project/pdfkit/0.4.1/
然后這個代碼:
import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')
我真的不完全理解你想要做什么,但如果這有幫助,請發表評論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.