使用 RSelenium 将页面打印为 PDF [英] Print as PDF of a page using RSelenium

查看:55
本文介绍了使用 RSelenium 将页面打印为 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过在 R 中使用 Selenium 将对象保存为 PDF 来收集数据.我在到达打印对话框窗口时遇到了一些问题.我的策略是尝试跳过打印提示/对话框,但我无法让它工作.我试过这个:

I'm trying to collect data by saving the objects as PDF's using Selenium in R. I'm having some issues when reaching to the print-dialog window. My strategy has been to try to skip the print prompt/dialog but I can't get it to work. I tried this:

cprof <- list(
  chromeOptions = 
    list(prefs = list(
      'profile.default_content_settings.popups' = 0L,
      'download.prompt_for_download' = FALSE,
      'download.default_directory' = 'C:\\Users\\User\\Documents\\Text',
      'download.directory_upgrade' = TRUE,
      'plugins.plugins_disabled" = 'Chrome PDF Viewer',
      'print.prompt' = FALSE
      )
    )
  )

但是打印对话框/提示仍然存在.我知道一种方法是使浏览器处于 kiosk 模式,但我还没有找到在 R 中执行此操作的方法(关于如何在 Java 或 Python 中执行此操作有几个线程).

But the print dialog/prompt is still there. I know one way is to make the browser to be in kiosk-mode but I haven't found a way to do this in R (there are several threads about how to do it in Java or Python).

提前致谢!

推荐答案

我也需要解决这个问题.基于这些文档(请参阅已识别的功能"部分中的args"条目,其中参考 这些附加文档),以下对我有用:

I needed to figure this out, as well. Based on these docs (see the "args" entry in the "Recognized capabilities" section, which references these additional docs), the following worked for me:

eCaps <- list(
    chromeOptions = 
        list(args = list('--kiosk-printing')))

remDr <- remoteDriver(remoteServerAddr = "localhost", port = 4445L, 
                      browserName = "chrome", extraCapabilities = eCaps)

    

然后,一旦您打开一个包含您要保存的 PDF 的窗口,以下内容会自动将其保存到您指定的下载目录,而无需任何额外提示:

Then, once you open a window with the PDF you want to save, the following automatically saves it to your specified download directory without any additional prompts:

remDr$findElements('css selector', 'html')[[1]]$sendKeysToElement(list(key='control', 'p'))

这篇关于使用 RSelenium 将页面打印为 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆