R 将 RSelenium 驱动程序环境作为函数参数传递
R passing RSelenium driver environment as function argument
我可能没有看到明显的东西,无论如何我想创建函数来自动从远程驱动程序已经处理的 url 中提取文本。我想将 xpath 表达式和可以找到远程驱动程序的环境作为函数参数传递
library(RSelenium)
url="http://whosebug.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
但是如果我直接在环境中执行从错误中提取的调用
消息它将起作用。
tgt<-remdir$findElements(using = "xpath","//*")
我尝试将 as environment 也传递给 remdir 本身,因为它是一个环境,但这根本不算数,该函数在调用构建后卡在同一点。
我不知道什么?
不确定你到底想做什么。但是 eval
似乎不是答案。您应该将 remoteDriver
对象传递给您的函数:
library(RSelenium)
url="http://whosebug.com/search?q=r+program"
remdir<-remoteDriver(remoteServerAddr = "localhost", port = 4444, browserName = "firefox")
remdir$open()
remdir$navigate(url)
fun.XpathExtractText<-function(xp, remdir)
{
cat("\ncheck if session open\n")
#look in env for an open session
if (is.na(remdir$sessionid))
stop("ERROR NO SESSION ID open new one")
cat("session found\n")
#accept xpath expression as is
cat("calling\n")
tgt <- remdir$findElements(using = "xpath",as.character(xp))
cat("Target locked\n")
txt<-lapply(tgt,function(c){c$getElementText()})
return(txt)
}
抱歉,我不够清楚,无论如何,我正在尝试构建一个可以采用 xpath 和 return 找到的元素文本的函数,我想扩展它以获取属性值和其他东西,将所有内容都放入一行命令中,并有机会以编程方式更改 xpath。其中一部分是我为理解评估、替代等所做的一种练习……至少这是第一个想法。
无论如何这是有效的:
fun.XpathExtractText<-function(xp,dir)
{
#look in env for an open session
if (is.na(dir$sessionid))
stop("ERROR NO SESSION ID open new one")
#accept xpath expression as is
xp <- substitute(xp)
txt<-c()
tgt<-dir$findElements(using = "xpath",xp)
txt<-lapply(tgt,function(c){c$getElementText()})
return(txt)
}
只需将 remoteDriver 的名称作为 dir 传递。
我可能没有看到明显的东西,无论如何我想创建函数来自动从远程驱动程序已经处理的 url 中提取文本。我想将 xpath 表达式和可以找到远程驱动程序的环境作为函数参数传递
library(RSelenium)
url="http://whosebug.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
但是如果我直接在环境中执行从错误中提取的调用 消息它将起作用。
tgt<-remdir$findElements(using = "xpath","//*")
我尝试将 as environment 也传递给 remdir 本身,因为它是一个环境,但这根本不算数,该函数在调用构建后卡在同一点。 我不知道什么?
不确定你到底想做什么。但是 eval
似乎不是答案。您应该将 remoteDriver
对象传递给您的函数:
library(RSelenium)
url="http://whosebug.com/search?q=r+program"
remdir<-remoteDriver(remoteServerAddr = "localhost", port = 4444, browserName = "firefox")
remdir$open()
remdir$navigate(url)
fun.XpathExtractText<-function(xp, remdir)
{
cat("\ncheck if session open\n")
#look in env for an open session
if (is.na(remdir$sessionid))
stop("ERROR NO SESSION ID open new one")
cat("session found\n")
#accept xpath expression as is
cat("calling\n")
tgt <- remdir$findElements(using = "xpath",as.character(xp))
cat("Target locked\n")
txt<-lapply(tgt,function(c){c$getElementText()})
return(txt)
}
抱歉,我不够清楚,无论如何,我正在尝试构建一个可以采用 xpath 和 return 找到的元素文本的函数,我想扩展它以获取属性值和其他东西,将所有内容都放入一行命令中,并有机会以编程方式更改 xpath。其中一部分是我为理解评估、替代等所做的一种练习……至少这是第一个想法。
无论如何这是有效的:
fun.XpathExtractText<-function(xp,dir)
{
#look in env for an open session
if (is.na(dir$sessionid))
stop("ERROR NO SESSION ID open new one")
#accept xpath expression as is
xp <- substitute(xp)
txt<-c()
tgt<-dir$findElements(using = "xpath",xp)
txt<-lapply(tgt,function(c){c$getElementText()})
return(txt)
}
只需将 remoteDriver 的名称作为 dir 传递。