Rselenium / dialog box

北战南征 提交于 2021-01-29 15:15:21

问题


I am very new to RSelenium. I have a list of Finnish companies and I would like to extract the corresponding Business ID on the web page https://tietopalvelu.ytj.fi/yrityshaku.aspx?kielikoodi=3

A simple version of my R-code is the following:

library(RSelenium)
name_company <- c("nokia", "test")
driver <- rsDriver(browser= 'firefox', port = 16L)

remote_driver <- driver[["client"]] 
remote_driver$navigate("https://tietopalvelu.ytj.fi/yrityshaku.aspx?kielikoodi=3")

input1 <- remote_driver$findElement(using = 'id', value = '_ctl0_cphSisalto_hakusana')
input1$sendKeysToElement(list(name_company[1])) # Name of the company

button_element1 <- remote_driver$findElement(using = 'id', value = '_ctl0_cphSisalto_sanahaku')
button_element1$clickElement() # Tick the box "word search"

button_element2 <- remote_driver$findElement(using = 'id', value = '_ctl0_cphSisalto_Hae')
button_element2$clickElement()

output <- remote_driver$findElement(using = "id", value="search-result")
output <- output$getElementText() # output including Business ID

It works well when the name of the company is "nokia" (first element of the vector in my example) but when it's "test" I get the dialog box: "No matches were find with search options".

I would like to loop this piece of code on the vector of all firm names I have. (e.g c("nokia", "kone", "blabla", ...) ) How to ensure that the loop is not stopped by the dialog box? I hope it's clear.

Thank you.

Alexis


回答1:


You should use the tryCatch function. It is able to manage the errors that the system gives you back. Below a simple example about how to implement it in your code.

Here the structure of the function

tryCatch(                       
  expr = {                      # Specifying expression
    1 + 1
    message("Everything was fine.")
  },
  error = function(e){          # Specifying error message
    message("There was an error message.")
  },
  warning = function(w){        # Specifying warning message
    message("There was a warning message.")
  },
  finally = {                   # Specifying final message
    message("tryCatch is finished.")
  }
)

Here how to implement the trycatch function in your code

library(RSelenium)
name_company <- c("nokia", "test")
driver <- rsDriver(browser= 'firefox', port = 16L)
remote_driver <- driver[["client"]] 
remote_driver$navigate("https://tietopalvelu.ytj.fi/yrityshaku.aspx?kielikoodi=3")

for (i in 1:length(name_company)) {
  
# different operations 

tryCatch(expr = {
input1 <- remote_driver$findElement(using = 'id', value = '_ctl0_cphSisalto_hakusana')
input1$sendKeysToElement(list(name_company[i])) # Name of the company

button_element1 <- remote_driver$findElement(using = 'id', value = '_ctl0_cphSisalto_sanahaku')
button_element1$clickElement() # Tick the box "word search"

button_element2 <- remote_driver$findElement(using = 'id', value = '_ctl0_cphSisalto_Hae')
button_element2$clickElement()

output <- remote_driver$findElement(using = "id", value="search-result")
output <- output$getElementText() # output including Business ID
}, 
error = function(e){          # Specifying error message
  message("There was an error message.")
})
  
# other operations 
}


来源:https://stackoverflow.com/questions/65625579/rselenium-dialog-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!