Rselenium / 对话框
Rselenium / dialog box
我是 RSelenium 的新手。我有一份芬兰公司的名单,我想在网页上提取相应的企业 ID https://tietopalvelu.ytj.fi/yrityshaku.aspx?kielikoodi=3
我的 R 代码的一个简单版本如下:
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
当公司名称为“nokia”(在我的示例中是矢量的第一个元素)时,它运行良好,但当它为“test”时,我得到对话框:“未找到与搜索选项的匹配项”。
我想在我拥有的所有公司名称的向量上循环这段代码。 (例如 c("nokia", "kone", "blabla", ...) )
如何保证循环不被对话框停止?
我希望这很清楚。
谢谢。
亚历克西斯
您应该使用 tryCatch
函数。它能够管理系统返回给您的错误。
下面是一个关于如何在您的代码中实现它的简单示例。
这里是函数的结构
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.")
}
)
这里是如何在您的代码中实现 trycatch
功能
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
}
我是 RSelenium 的新手。我有一份芬兰公司的名单,我想在网页上提取相应的企业 ID https://tietopalvelu.ytj.fi/yrityshaku.aspx?kielikoodi=3
我的 R 代码的一个简单版本如下:
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
当公司名称为“nokia”(在我的示例中是矢量的第一个元素)时,它运行良好,但当它为“test”时,我得到对话框:“未找到与搜索选项的匹配项”。
我想在我拥有的所有公司名称的向量上循环这段代码。 (例如 c("nokia", "kone", "blabla", ...) ) 如何保证循环不被对话框停止? 我希望这很清楚。
谢谢。
亚历克西斯
您应该使用 tryCatch
函数。它能够管理系统返回给您的错误。
下面是一个关于如何在您的代码中实现它的简单示例。
这里是函数的结构
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.")
}
)
这里是如何在您的代码中实现 trycatch
功能
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
}