尝试和捕获不起作用

时间:2015-11-04 08:33:04

标签: r exception try-catch

我使用以下代码:

require(RSelenium)
startServer()
remDr <- remoteDriver()
remDr$open()
for (i in 1:length(url_list)) {
  url <- url_list[i]
  x <- remDr$navigate(url)
  if (class(x)=="try-error" ) {
     cat("something happened at ",something,"\n")
     next
  } else {
  remDr$refresh()
  Sys.sleep(2) #allow browser to catch up
    result <-  remDr$executeScript('return window.location;')
  #other code
}
}

我尝试使用方法尝试捕获错误,即使存在错误,我的代码也会继续。但是我的命令停止执行并出错:

Error:   Summary: UnknownError
     Detail: An unknown server-side error occurred while processing the command.
     class: org.openqa.selenium.WebDriverException

代码中有什么问题吗?

即使加载了某些网址,也会发生此错误。

1 个答案:

答案 0 :(得分:0)

试试这个:

x <- tryCatch({
        remDr$navigate(url)
     }, error = function(e) {
          print(paste0("error: ", e, " at: ", i)
          return(NULL)
     })

if (!is.null(x)) {
   remDr$refresh()
    Sys.sleep(2) #allow browser to catch up
    result <-  remDr$executeScript('return window.location;')
}