使用应用循环的 rselenium 驱动程序错误
rselenium driver error using apply loop
我正在使用 rselenium 在 Firefox 中自动进行页面导航。我的 rscript 为不同的条件导入数据框并创建一个自定义函数,然后使用 apply 命令调用该函数。在函数中,每一列都是通过这样的方式引用的:
#create the function
example <- function(dat) {
webElem$sendKeysToElement(list(dat[[column1]]))
#Enters text from the column in the web driver
}
apply(df, 1, example) #Should repeat the function for each row of the data
我在应用函数中引用了多个列,以便来自每个列的数据可以与 Web 驱动程序进行不同的交互 - 例如,dat[[column2]]
、dat[[column3]]
、等等。大多数情况下,我只是发送键(findElement(using = 'tag name', 'body')
之后的制表符和箭头键,或者单击我使用 findElement
函数及其 ID 找到的按钮。
我执行了脚本,第一次运行时运行良好,但重新开始时出现错误。我希望 selenium 再次打开起始 url,并使用数据框的下一行重复导航。但是,发生的是 r 锁定或报告错误:
Error: Summary: NoSuchDriver
Detail: A session is either terminated or not started
class: org.openqa.selenium.NoSuchSessionException
Further Details: run errorDetails method
我认为它可以追溯到函数开头的 findElements
调用或 sendKeysToElement
调用。我试过关闭、退出和重新初始化远程驱动程序,但似乎没有什么不同。是否有任何故障排除提示?或者在 r 中使用 selenium 进行重复导航的好方法?
我能够通过将我的代码从 apply
函数重铸为 for(...)
循环来解决这个问题。我创建的函数变化很小。
我在循环的开头启动了网络驱动程序
remDr <- remoteDriver(extraCapabilities = list(marionette = TRUE))
remDr$open(silent = TRUE)
remDr$navigate(url)
在整个循环中,我小心地将变量传递给依赖于 rselenium
的方法,例如 findElement
和 sendKeysToElement
。看起来像这样:
nextBtn(pageBody, remDr)
这是 shorthand 用于:
nextBtn <- function(element=pageBody, driver=remDr) {
Sys.sleep(.5)
driver$findElement(using = 'id',value = "NextButton")$clickElement()
Sys.sleep(2.5)
}
我认为因为这些方法是在单独的函数中调用的,所以它们需要指向正确的驱动程序元素(即使我曾尝试设置默认值)
在 for
循环结束时,代码关闭了网络驱动程序:
remDr$close()
remDr$quit()
循环的实际调用是
for (i in 1:nrow(df)) {
surveys(df[i,])
}
最终结果是循环起作用了。它每次都会打开一个新的驱动程序实例,但没有错误。对特定列的引用仍然使用 df[[mold0to2]]
.
我正在使用 rselenium 在 Firefox 中自动进行页面导航。我的 rscript 为不同的条件导入数据框并创建一个自定义函数,然后使用 apply 命令调用该函数。在函数中,每一列都是通过这样的方式引用的:
#create the function
example <- function(dat) {
webElem$sendKeysToElement(list(dat[[column1]]))
#Enters text from the column in the web driver
}
apply(df, 1, example) #Should repeat the function for each row of the data
我在应用函数中引用了多个列,以便来自每个列的数据可以与 Web 驱动程序进行不同的交互 - 例如,dat[[column2]]
、dat[[column3]]
、等等。大多数情况下,我只是发送键(findElement(using = 'tag name', 'body')
之后的制表符和箭头键,或者单击我使用 findElement
函数及其 ID 找到的按钮。
我执行了脚本,第一次运行时运行良好,但重新开始时出现错误。我希望 selenium 再次打开起始 url,并使用数据框的下一行重复导航。但是,发生的是 r 锁定或报告错误:
Error: Summary: NoSuchDriver
Detail: A session is either terminated or not started
class: org.openqa.selenium.NoSuchSessionException
Further Details: run errorDetails method
我认为它可以追溯到函数开头的 findElements
调用或 sendKeysToElement
调用。我试过关闭、退出和重新初始化远程驱动程序,但似乎没有什么不同。是否有任何故障排除提示?或者在 r 中使用 selenium 进行重复导航的好方法?
我能够通过将我的代码从 apply
函数重铸为 for(...)
循环来解决这个问题。我创建的函数变化很小。
我在循环的开头启动了网络驱动程序
remDr <- remoteDriver(extraCapabilities = list(marionette = TRUE)) remDr$open(silent = TRUE) remDr$navigate(url)
在整个循环中,我小心地将变量传递给依赖于
rselenium
的方法,例如findElement
和sendKeysToElement
。看起来像这样:nextBtn(pageBody, remDr)
这是 shorthand 用于:
nextBtn <- function(element=pageBody, driver=remDr) { Sys.sleep(.5) driver$findElement(using = 'id',value = "NextButton")$clickElement() Sys.sleep(2.5) }
我认为因为这些方法是在单独的函数中调用的,所以它们需要指向正确的驱动程序元素(即使我曾尝试设置默认值)
在
for
循环结束时,代码关闭了网络驱动程序:remDr$close() remDr$quit()
循环的实际调用是
for (i in 1:nrow(df)) {
surveys(df[i,])
}
最终结果是循环起作用了。它每次都会打开一个新的驱动程序实例,但没有错误。对特定列的引用仍然使用 df[[mold0to2]]
.