RSelenium: Try if findElement succeeds, then click on it, error: "object of type 'S4' is not subsettable"

RSelenium: Try if findElement succeeds, then click on it, error: "object of type 'S4' is not subsettable"

我正在用 RSelenium 抓取 https://www.tandfonline.com/loi/sabo20

我想单击每个 li 的文本以 20152021 年结束。 xpath 运行良好。

一年(例如 2021)可能会丢失,这就是我使用 try() 方法的原因。

  URL <- "https://www.tandfonline.com/loi/sabo20"

  # open RSelenium
  rD <- RSelenium::rsDriver(browser = "chrome", chromever = "90.0.4430.24", port = 4546L, verbose = F)

  remDr <- rD[["client"]]
  remDr$navigate(URL)
  Sys.sleep(4)

  for (yyyy in c(2015:2021)) {
    error <- "Error : \t Summary: NoSuchElement\n \t Detail: An element could not be located on the page using the given search parameters.\n \t class: org.openqa.selenium.NoSuchElementException\n\t Further Details: run errorDetails method\n"

    volumes <- try(unlist(
      remDr$findElement(
        using = "xpath",
        paste0(
          "//li[substring(@id, string-length(@id) - string-length('",
          yyyy,
          "') +1) = '",
          yyyy,
          "']/div"
        )
      )
    ))
    
      if(volumes[1] == error)
        break;
    
    volumes$clickElement()
  }

不幸的是,如果 findElement() 成功,if(volumes[1] == error) 会导致错误:

卷中的错误[1]:'S4' 类型的对象不是子集

clickElement()之前如何检查remDr$findElement()是否成功?

解决方案可能是trycatch

下面举个例子。

URL <- "https://www.tandfonline.com/loi/sabo20"

# open RSelenium
rD <- RSelenium::rsDriver(browser = "chrome", chromever = "90.0.4430.24", port = 4546L, verbose = F)

remDr <- rD[["client"]]
remDr$navigate(URL)
Sys.sleep(4)

for (yyyy in c(1983:2021)) {
tryCatch(expr = {   
  volumes <- unlist(
    remDr$findElement(
      using = "xpath",
      paste0(
        "//li[substring(@id, string-length(@id) - string-length('",
        yyyy,
        "') +1) = '",
        yyyy,
        "']/div"
      )
    )
  )
  volumes$clickElement()
  Sys.sleep(4)
  },
  error =function(e){          
    message("if you want you can break, but it not necessary")
  })
}