sendKeysToActiveElement无法按预期运行,RSelenium

时间:2017-09-20 03:00:04

标签: r rselenium

我试图通过右键单击并选择"另存为"来尝试在页面中下载PDF,但我显然没有做正确的操作,如右键菜单弹出但箭头移动不会发生。

欢迎任何建议。

编辑:对不起,我是新来的,这是一个比下面原版更容易重现的例子。在这个例子中,我试图右键单击,向下箭头指向"另存为"并按Enter键。右键单击可以正常工作,但没有别的。

library(RSelenium)
driver <- rsDriver()
remDr <- driver[["client"]]
remDr$navigate("https://www.google.com/")
webElem <- remDr$findElement("css", "html")
webElem$click(2)
webElem$sendKeysToElement(list(key = "down_arrow", key = "down_arrow", key = "enter"))

以下老例子。打开浏览器,导航到表单页面,选择2016年的所有结果,然后单击&#34;查看文档&#34;第一行中的按钮将页面打开到PDF。 :

driver <- rsDriver()
remDr <- driver[["client"]]
remDr$navigate("https://portal.sos.state.nm.us/FinancialDisclosure/search.aspx")
webElem <- remDr$findElement(using = 'id', value = "ContentPlaceHolder1_ddlYear")
webElem$sendKeysToElement(list("2016", key = "enter"))
webElem <- remDr$findElement(using = 'id', value = "ContentPlaceHolder1_btnSearch")
webElem$clickElement()
webElem <- remDr$findElement(using = 'id', value = "ContentPlaceHolder1_gvSummary_btnShow_0")
webElem$getWindowHandles()
webElem$getCurrentWindowHandle() #Here I identify the window handle of the popup and paste it in "x" below:
webElem$switchToWindow("x")

这一切都有效,我在正确的窗口标签中工作,但是在下面的右键单击打开右键单击菜单后,最后一行没有达到我的预期,这是突出显示&#34;另存为&#34;并点击&#34;输入。&#34;相反,整个屏幕向下移动,好像发生了一个向下箭头,然后没有别的。

webElem$click(2)
webElem$sendKeysToActiveElement(list(key = 'down_arrow', key = 'down_arrow', key = 'enter'))

1 个答案:

答案 0 :(得分:0)

您将无法与另存为对话框进行交互。您可以按如下方式保存文件(赢取10个文件路径等操作系统依赖):

library(RSelenium)
eCaps <- list(
  chromeOptions = 
    list(prefs = list(
      "profile.default_content_settings.popups" = 0L,
      "download.prompt_for_download" = FALSE,
      "download.default_directory" = "C:/temp/chromeDL",
      "download.directory_upgrade" = TRUE,
      "plugins.always_open_pdf_externally" = TRUE
    )
    )
)
driver <- rsDriver(extraCapabilities = eCaps)
remDr <- driver[["client"]]
remDr$navigate("https://portal.sos.state.nm.us/FinancialDisclosure/search.aspx")
webElem <- remDr$findElement(using = 'id', value = "ContentPlaceHolder1_ddlYear")
webElem$sendKeysToElement(list("2016", key = "enter"))
webElem <- remDr$findElement(using = 'id', value = "ContentPlaceHolder1_btnSearch")
webElem$clickElement()
webElem <- remDr$findElement(using = 'id', value = "ContentPlaceHolder1_gvSummary_btnShow_0")
webElem$clickElement()

file.rename("C:/temp/chromeDL/FDSDocument.ashx", 
            "C:/temp/chromeDL/testpdf1.pdf")

....
....
# clean up
rm(driver)
gc()