Problem downloading file from a new tab with Rselenium

Viewed 159

I am new with Rselenium and have realized there is a lot of help about problems with selenium in python or java but not in R. I am trying to download excel files from this web:

http://app.ceplan.gob.pe/ConsultaCEPLAN/Reporte/Maestro/rptMaestroObjetivo.aspx?pAnoEje=2022&pTipoGobierno=E&pSector=10&pPliego=510

I am using dockers and I got this chunck of code:

shell('docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome:3.141.59-20200326')

remDr = remoteDriver(
  remoteServerAdd = "localhost",
  port = 4444L,
  browser = "chrome"
)

remDr$open()

Then I set the web page I am interested:

remDr$navigate("http://app.ceplan.gob.pe/ConsultaCEPLAN/Reporte/Maestro/rptMaestroObjetivo.aspx?pAnoEje=2022&pTipoGobierno=E&pSector=10&pPliego=510")

Then I need to select the diskette icon and after that, the "excel" option which will generate another tab and then save the excel file (I already configured chrome to not ask when it downloads files).

enter image description here

So when I inspect the element of the excel option I got the xpath "//*[@id="ReportViewer1_ctl05_ctl04_ctl00_Menu"]/div[1]/a" so with this I code the next lines in order to click the "excel" option to download the file I am interested in.

boton_guardar = remDr$findElement(using = "xpath", '//*[@id="ReportViewer1_ctl05_ctl04_ctl00_Menu"]/div[1]/a')
boton_guardar$clickElement()

The console run everything but I got no results. If I do the exercise manually when i clic the "excel" option Chrome opens a new tab, downloads the file and then closes the new tab.

I want to know what my code needs to finish the task or what I coded wrong.

Thanks in advance.

2 Answers

From my brief research, it looks like downloading files with rselenium is an issue.

If you can find a way to get the url of the file after you click to download it, then you could try to use download.file()

It would look something like this:

## your code to navigate to the page
shell('docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome:3.141.59-20200326')

remDr = remoteDriver(
  remoteServerAdd = "localhost",
  port = 4444L,
  browser = "chrome"
)

remDr$open()

remDr$navigate("http://app.ceplan.gob.pe/ConsultaCEPLAN/Reporte/Maestro/rptMaestroObjetivo.aspx?pAnoEje=2022&pTipoGobierno=E&pSector=10&pPliego=510")

## New code to click the link and get the URL of the file you want to download would go here, save the url as X

## Download the file
download.file(X, destfile = "Name you want to save file as.csv", method = "curl")

Check here for how to use the download.file() function

Hopefully my idea gets you on the right path!

I was able to download the excel file by,

library(RSelenium)
driver <- rsDriver(browser = c("chrome"), port = 4341L)
remDr <- driver$client

# Select the dropdown menu,

remDr$findElement(using = "xpath",'//*[@id="ReportViewer1_ctl05_ctl04_ctl00"]') -> dropdown
dropdown$clickElement()

# Select the Excel file 

remDr$findElement(using = "xpath",'//*[@id="ReportViewer1_ctl05_ctl04_ctl00_Menu"]/div[1]') -> exceldownload
exceldownload$clickElement()
Related