Search code examples
rseleniumrselenium

RSelenium message:no such element: Unable to locate element


I intend to download and clean databases using RSelenium. I am able to open the link however I am having trouble downloading and opening the database. I believe the xpath is right but when I try to open I receive the following error

Selenium message:no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="ESTBAN_AGENCIA"]"}

My code is the following:

dir <- getwd()
file_path <- paste0(dir,"\\DataBase") %>% str_replace_all("/", "\\\\\\")

eCaps <- list(
  chromeOptions = 
    list(prefs = list('download.default_directory' = file_path))
)

system("taskkill /im java.exe /f", intern=FALSE, ignore.stdout=FALSE)
#Creating server
rD <-  rsDriver(browser = "chrome", 
                chromever = "101.0.4951.15",
                port = 4812L,
                extraCapabilities = eCaps)  

#Creating the driver to use R

remDr <- remoteDriver(
  remoteServerAddr = "localhost",
  browserName = "chrome",
  port = 4812L) 

#Open server
remDr$open()
#Navegating in the webpage of ESTABAN
remDr$navigate("https://www.bcb.gov.br/acessoinformacao/legado?url=https:%2F%2Fwww4.bcb.gov.br%2Ffis%2Fcosif%2Festban.asp")

##Download 
remDr$findElement(using ="xpath", '//*[@id="ESTBAN_AGENCIA"]/option[1]')

Solution

  • The element you are trying to access is inside an iframe and you need switch that iframe first in order to access the element.

    enter image description here

    remDr$navigate("https://www.bcb.gov.br/acessoinformacao/legado?url=https:%2F%2Fwww4.bcb.gov.br%2Ffis%2Fcosif%2Festban.asp")
    
    #Switch to Iframe
    webElem <- remDr$findElement("css", "iframe#framelegado")
    remDr$switchToFrame(webElem)
    
    ##Download 
    remDr$findElement(using ="xpath", '//*[@id="ESTBAN_AGENCIA"]/option[1]')