python - 硒打印不起作用(打印到 PDF)
问题描述
我想要 selenium:打开一个页面,休眠 10 秒(确保所有内容都已加载),然后打印网页。这两种解决方案都不是,使用javascript打印页面,或者使用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% 的时间?
解决方案
您可以尝试使用以下命令,而不是使用 selenium:
https://pypi.org/project/pdfkit/0.4.1/
然后这段代码:
import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')
推荐阅读
- r - 数据框中两列之间定义的范围之间的ggplot线或条形图
- c# - 天蓝色会员 System.Data.SqlClient.SqlException:Azure 中用户“{your_username}”的登录失败
- flutter - 如何在 onPressed() 中动态更改凸起按钮的背景颜色
- python - 如何从 Flask 中的 API 获取数据?
- c - Connect 在 C 中不断失败,出现错误 10049
- wpf - 使用 prism 7 显示启动画面的最佳位置在哪里
- java - 我们可以显式地创建单独的测试用例并在 JUnit 中运行它们吗?
- android - 在使用 RX 返回之前填充一些列表
- python - 如何使用 SQLAlchemy ORM 将孩子从一个父母复制到另一个父母?
- python - Tensorflow中的复值分布?