Rselenium/对话框 [英] Rselenium / dialog box

查看:43
本文介绍了Rselenium/对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 RSelenium 很陌生.我有一个芬兰公司列表,我想在网页上提取相应的企业 ID https://tietopalvelu.ytj.fi/yrityshaku.aspx?kielikoodi=3

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

我的 R 代码的简单版本如下:

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

当公司名称为nokia"时效果很好;(在我的例子中向量的第一个元素)但是当它是测试"时我看到对话框:没有找到匹配的搜索选项".

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".

我想在我拥有的所有公司名称的向量上循环这段代码.(例如 c("nokia", "kone", "blabla", ...) )如何确保循环不被对话框终止?我希望它很清楚.

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.

谢谢.

亚历克斯

推荐答案

您应该使用 tryCatch 函数.它能够管理系统返回给您的错误.下面是一个关于如何在代码中实现它的简单示例.

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.

这里是函数的结构

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函数

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 
}

这篇关于Rselenium/对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆