首页 > 解决方案 > 退出 R 中的函数

问题描述

我正在编写一个从 R 中的 Gmail 帐户中提取用户名的网络抓取项目。我编写了一个代码,其中 e 是用户名列表。函数 f 包含用于抓取的代码 & for 循环将索引传递给函数 f,使用索引我们将用户名发送到远程浏览器,然后单击下一步。如果我的用户名是正确的控制将转到下一页。如果错了,我会得到一行用户名错误。如果我的函数在用户名中找到错误元素,那么我想退出函数并转到 for 循环。我怎样才能在 R 中做到这一点?有没有其他方法可以获得我的预期输出?

代码-

e = list of Gmail username

f <- function(i){
 #Username
u <- remDr$findElement("css","value")
u$sendKeysToElement(list(e[i]))

 #click next
n <- remDr$fundElement("css","value")
n$sendKeysToElement(list("Enter"))

##if user name is wrong exit function & go to for loop
check <- tryCatch(remDr$findElemnt("css","value"), 
       error = function(e) print("error"))

if(typeof(check) =="S4"){
 ##what to do to exit function & go to for loop

 }else{
 print("continue")
}

for(i in sew(1,4))
 {
    f(i)
  }

标签: rweb-scraping

解决方案


推荐阅读