簡體   English   中英

Selenium 打印不工作(打印為 PDF)

[英]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% 正常工作的方法?

相關問題: 在 chrome 中自動打印/保存網頁為 pdf - python 2.7

你可以嘗試使用這個來代替使用硒:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM