【问题标题】:How to download embedded PDF files from webpage using RSelenium?如何使用 RSelenium 从网页下载嵌入的 PDF 文件?
【发布时间】:2021-05-15 20:11:06
【问题描述】:

编辑:从我目前收到的 cmets 中,我设法使用 RSelenium 访问我正在寻找的 PDF 文件,使用以下代码:

library(RSelenium)
driver <- rsDriver(browser = "firefox")
remote_driver <- driver[["client"]]
remote_driver$navigate("https://www.rad.cvm.gov.br/enetconsulta/frmGerenciaPaginaFRE.aspx?CodigoTipoInstituicao=1&NumeroSequencialDocumento=62398")
# It needs some time to load the page
option <- remote_driver$findElement(using = 'xpath', "//select[@id='cmbGrupo']/option[@value='PDF|412']")
option$clickElement()

现在,我需要 R 来单击下载按钮,但我无法做到。我试过了:

button <- remote_driver$findElement(using = "xpath", "//*[@id='download']")
button$clickElement()

但我收到以下错误:

Selenium message:Unable to locate element: //*[@id="download"]
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '4.0.0-alpha-2', revision: 'f148142cf8', time: '2019-07-01T21:30:10'

Erro:    Summary: NoSuchElement
 Detail: An element could not be located on the page using the given search parameters.
 class: org.openqa.selenium.NoSuchElementException
 Further Details: run errorDetails method

有人能说出这里出了什么问题吗? 谢谢!

原问题:

我有几个网页需要下载嵌入的 PDF 文件,我正在寻找一种使用 R 实现自动化的方法。这是其中一个网页:https://www.rad.cvm.gov.br/enetconsulta/frmGerenciaPaginaFRE.aspx?CodigoTipoInstituicao=1&NumeroSequencialDocumento=62398 这是 CVM(Comissão de Valores Mobiliários,巴西相当于美国证券交易委员会 - SEC)的网页,用于下载财务报表附注(Notas Explicativas)巴西公司。

我尝试了几个选项,但该网站的构建方式似乎难以提取直接链接。 我尝试了Downloading all PDFs from URL 中的建议,但html_nodes(".ms-vb2 a") %&gt;% html_attr("href") 产生了一个空字符向量。 同样,当我在这里尝试https://www.samuelworkman.org/blog/scraping-up-bits-of-helpfulness/ 中的方法时,html_attr("href") 会生成一个空向量。

我不习惯在 R 中使用网页 scraping 代码,所以我无法弄清楚发生了什么。 感谢您的帮助!

【问题讨论】:

  • 该页面上没有链接 pdf 文件,因此 pkg-rvest 似乎不太可能成为解决方案。我猜你不习惯用任何语言进行网页抓取。该页面的源代码是用 Javascript 编写的,用于打开可以选择 pdf 文件的对话框窗口的按钮名为“btnGeraRelatorioPDF”。我怀疑您将需要获得一个包,该包具有比 rvest 提供的更多的与网页交互的功能。 Selenium 包曾经是 goto 解决方案,但我认为其他浏览器导航工具也可用。使用浏览器查看页面源代码。
  • 使用“[r] javascript dialog”搜索 SO 我发现这个可能有用的答案:stackoverflow.com/questions/29759438/rselenium-popup/… 这是来自同一贡献者的另一个可能有用的答案,@JDHarrison:stackoverflow.com/questions/38156180/…
  • 根据@IRTFM 的问题建议,我正在使用 RSelenium,但我无法弄清楚如何在第一个下拉菜单中选择“Notas Explicativas”,它会生成嵌入的我要下载的 PDF 文件。
  • @RonakShah,打开页面后library(RSelenium) driver &lt;- rsDriver(browser = "firefox") remote_driver &lt;- driver[["client"]] remote_driver$navigate("https://www.rad.cvm.gov.br/enetconsulta/frmGerenciaPaginaFRE.aspx?CodigoTipoInstituicao=1&amp;NumeroSequencialDocumento=62398")我想在第一个下拉菜单中选择“Notas Explicativas”选项,生成我要下载的pdf文件.
  • 我想我没有仔细阅读您的注释,并且我之前指定的按钮名称不正确。在我看来,您需要首先选择该菜单带(或条带?不确定正确的术语)本身。在 Chrome 中,在该菜单条上执行“检查”会突出显示以 &lt;select name="cmbGrupo" onchange="javascript:setTimeout('__doPostBack(\'cmbGrupo\',\'\')', 0)" id="cmbGrupo" label="Você esta vendo:" showlabel="true" style="width:49%;"&gt; 开头的部分,然后在其下方列出选项。

标签: r rvest rselenium


【解决方案1】:

如果有人遇到与我相同的问题,我将发布我使用的解决方案:

# set Firefox profile to download PDFs automatically
pdfprof <- makeFirefoxProfile(list(
  "pdfjs.disabled" = TRUE,
  "plugin.scan.plid.all" = FALSE,
  "plugin.scan.Acrobat" = "99.0",
  "browser.helperApps.neverAsk.saveToDisk" = 'application/pdf'))

driver <- rsDriver(browser = "firefox", extraCapabilities = pdfprof)
remote_driver <- driver[["client"]]
remote_driver$navigate("https://www.rad.cvm.gov.br/enetconsulta/frmGerenciaPaginaFRE.aspx?CodigoTipoInstituicao=1&NumeroSequencialDocumento=62398")
Sys.sleep(3) # It needs some time to load the page (set to 3 seconds)

option <- remote_driver$findElement(using = 'xpath', "//select[@id='cmbGrupo']/option[@value='PDF|412']") # select the option to open PDF file
option$clickElement()

# Find iframes in the webpage
web.elem <- remote_driver$findElements(using = "css", "iframe") # get all iframes in the webpage
sapply(web.elem, function(x){x$getElementAttribute("id")}) # see their names
remote_driver$switchToFrame(web.elem[[1]]) # Move to the first iframe (Formularios Filho)
web.elem.2 <- remote_driver$findElements(using = "css", "iframe") # get all iframes in the webpage
sapply(web.elem.2, function(x){x$getElementAttribute("id")}) # see their names
# The pdf Viewer iframe is the only one inside Formularios Filho
remote_driver$switchToFrame(web.elem.2[[1]]) # Move to the first iframe (pdf Viewer)
Sys.sleep(3) # It needs some time to load the page (set to 3 seconds)

# Download the PDF file
button <- remote_driver$findElement(using = "xpath", "//*[@id='download']")
button$clickElement() # download
Sys.sleep(3) # Need sometime to finish download and then close the window
remote_driver$close() # Close the window

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2019-11-21
  • 2019-08-10
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 2019-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多