【问题标题】:Selenium printing not working (Print to PDF)硒打印不起作用(打印到 PDF)
【发布时间】:2021-02-12 02:15:39
【问题描述】:

我想要 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% 工作的方法?

相关问题:Automate print/save web page as pdf in chrome - python 2.7

【问题讨论】:

    标签: python selenium selenium-webdriver printing


    【解决方案1】:

    你可以尝试使用这个来代替硒:

    https://pypi.org/project/pdfkit/0.4.1/

    然后是这段代码:

    import pdfkit
    pdfkit.from_url('http://google.com', 'out.pdf')
    

    【讨论】:

      猜你喜欢
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 2021-01-13
      • 2011-11-09
      相关资源
      最近更新 更多