我可能没有看到明显的东西,无论如何我想创建函数来自动从远程驱动程序已经处理的 url 中提取文本。我想将 xpath 表达式和可以找到远程驱动程序的环境作为函数参数传递
library(RSelenium)
url="http://stackoverflow.com/search?q=r+program"
remdir<-remoteDriver(remoteServerAddr = "localhost", port = 4444, browserName = "firefox")
remdir$open()
remdir$navigate(url)
env<-environment()
#env should be the environment in wich remdir exist (remdir itself?)
#xp the xpath expression to evaluate in the form "//*"
fun.XpathExtractText<-function(xp,env)
{
cat("\ncheck if session open\n")
#look in env for an open session
if ((eval(quote(is.na(remdir$sessionid)),envir = env)))
stop("ERROR NO SESSION ID open new one")
cat("session found\n")
#accept xpath expression as is
xp <- substitute(xp)
txt<-c()
#build the call to env
cat("calling\n")
call<-paste0("remdir$findElements(using = \"xpath\",\"",as.character(xp),"\")")
tgt<-eval(as.name(call),envir = env)
cat("Target locked\n")
txt<-lapply(tgt,function(c){c$getElementText()})
return(txt)
}
此函数的可能调用可能是fun.XpathExtractText("//*",env)
但在调用构建部分后不久出现错误消息:
Error in eval(expr, envir, enclos) :
object 'remdir$findElements(using = "xpath","//*")' not found
但是如果我直接在 env 中执行从错误消息中提取的调用,它将起作用。
tgt<-remdir$findElements(using = "xpath","//*")
我尝试将环境也作为环境传递 remdir 本身,因为它是一个环境,但这根本不算数,函数在调用构建后卡在同一点。我不知道什么?