【问题标题】:Rselenium / dialog box硒/对话框
【发布时间】:2021-01-08 08:24:36
【问题描述】:

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

我的 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", ...) ) 如何保证循环不被对话框停止? 我希望它很清楚。

谢谢。

亚历克西斯

【问题讨论】:

    标签: r popup try-catch rselenium


    【解决方案1】:

    您应该使用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 
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-20
      • 2012-08-22
      • 2011-09-11
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多