尝试捕获 RSelenium 错误

Try catch for RSelenium errors

我使用 RSelenium 检查一些页面。

我使用 for 循环访问此页面。

但是有时我会收到此错误:

Selenium message:stale element reference: element is not attached to the page document
  (Session info: chrome=64.0.3282.167)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 6.3.9600 x86_64)

Error:   Summary: StaleElementReference
     Detail: An element command failed because the referenced element is no longer attached to the DOM.
     Further Details: run errorDetails method

我的程序停止了。

因为我从来没有在 RSelenium 中使用过 try catch,我该如何编写在 for 循环中添加一个 try catch?

我从 python 示例中找到了这个:

for x in range(0, len(df.index)):
       try:
           twitter(df.username[x])
           print x
       except TweepError:
           pass

知道 TweepError 并传递错误并转到下一次迭代。

我在 r:

中有一个相同的 for 循环
for (i in 1:nrow(df)) {
  url <- df$page[i]
  testpage(url)
}

怎么可能做出像 python ir r 这样的东西?

我试过这样的事情:

for (i in 1:nrow(df)) {
  url <- df$page[i]
  try(testpage(url))
}

但我收到错误消息:

Error in testpage(url) : could not find function "testpage"

我已经在for之前运行了这个函数,我可以在环境变量中看到它并且名称是正确的。

最接近 python 的 trytryCatch:

for (i in 1:nrow(df)) {
  url <- df$page[i]
  tryCatch({
    testpage(url)
  }, error = function( err ) {
    print(paste( "Error:", err ))
    # You can run more code here
    # ...
  })
}