2

I'm attempting to download a PDF in a page via right-click and selecting "save as", but I'm clearly not doing something right, as the right-click menu pops up but the arrow moves do not happen.

Any advice welcome.

EDIT: Sorry, I'm new here, this is a more easily reproducible example than the original one below it. In this example I'm trying to right-click, arrow down to "save as" and hit enter. Right-click works but nothing else.

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"))

OLD EXAMPLE BELOW. Opens a browser, navigates to a form page, chooses all results from 2016, and clicks a "view document" button in the first row to open the page to the 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")

This all works and I'm working in the correct window tab, but after the following right-click which does open the right-click menu, the last line doesn't do what I expected it to, which is highlight "save as" and hit "enter." Instead the whole screen shifts down as if one down-arrow happened, then nothing else.

webElem$click(2)
webElem$sendKeysToActiveElement(list(key = 'down_arrow', key = 'down_arrow', key = 'enter'))
johnrroby
  • 88
  • 8

1 Answers1

0

You will not be able to interact with the save as dialog. You can save a file as follows (win 10 file path etc. OS dependant):

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()
jdharrison
  • 30,085
  • 4
  • 77
  • 89
  • Amazing, this answer came as I was composing that long and confusing edit. Works a charm, and I'd love to know more about what's going on with that eCaps variable. – johnrroby Sep 20 '17 at 14:39
  • The extra capabilities (eCap) argument passes the chrome browser a list of options. It tells it to download files automatically to the directory given. As of chrome 57 the way this works has changed. More detail here https://stackoverflow.com/questions/31672897/how-to-download-a-pdf-file-in-chrome-using-selenium-webdriver – jdharrison Sep 20 '17 at 14:57