Catch R Selenium error message and write it to log

↘锁芯ラ 提交于 2020-01-16 10:02:14

问题


I have a few scrapes via RSelenium scheduled. Sometimes the scraping failes and i would like to know the reason. I note that the error Messages (in red) are quite informative, but i dont know how to log them.

Lets say i provided a "non well formed URL".:

tryCatch(
  expr = remDr$navigate("i.am.not.an.url"),
  error = function(error){
    print(error)
    # write.table(error, file = ...)
  }  
)

This is what i get, but it doesnt give much specification on what triggered the error

<simpleError:    Summary: UnknownError
Detail: An unknown server-side error occurred while processing the command.
class: org.openqa.selenium.WebDriverException
Further Details: run errorDetails method>

This is more informative - but i dont manage to log it.

Selenium message:Target URL i.am.not.an.url is not well-formed.
Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:37:03'
System info: host: '9bc48e7a4511', ip: '172.17.0.4', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-1087-aws', java.version: '1.8.0_91'
Driver info: driver.version: unknown

What i tried:

Using the Error Handling Class. It includes very detailed specification of the error Messages and its meanings, but i dont manage to extract them given my current error.

errHandle = errorHandler(remDr)
errHandle$checkStatus(remDr)
errHandle$checkError(res = remDr)

Using a message handler from another #SO question:

messageHandler <- function(fun, ...) {
  zz <- textConnection("foo", "w", local = TRUE)
  sink(zz, type = "message")
  res <- fun(...)  
  sink(type = "message")
  close(zz)
  #handle messages
  list(res, messages = foo) 
}

wrongURL <- function() {
  remDr$navigate("mistake")
}

messageHandler(fun = wrongURL)

回答1:


I found a way via errorDetails():

tryCatch(
  expr = remDr$navigate("i.am.not.an.url"),
  error = function(error){
    return(remDr$errorDetails()$localizedMessage)
  }  
)


来源:https://stackoverflow.com/questions/56832150/catch-r-selenium-error-message-and-write-it-to-log

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