Search code examples
rdockerweb-scrapingrselenium

Problem downloading file from a new tab with Rselenium


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.


Solution

  • 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()