Try catch for RSelenium errors

时间:2018-02-26 17:50:25

标签: r rselenium

I use RSelenium to check some pages.

I visit this pages using a for loop.

However sometimes I receive this error:

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

and my program stops.

Because I have never used try catch in RSelenium how can I write to add a try catch in the for loop?

I found this from a python example:

for x in range(0, len(df.index)):
           print x
       except TweepError:

The know that TweepError and just pass the error and go to next iteration.

I have a same for loop in r:

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

How could it be possible to make something like python ir r?

I tried something like this:

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

but I receive the error :

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

I have runned the function before the for and I can see it in enviroment variables and the name is right.

1 个答案:

答案 0 :(得分:2)

最接近的python&#39; trytryCatch

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