RSelenium 将 profile/extraCapabilities 传递给服务器

RSelenium passing profile/extraCapabilities to server

我的下载代码停止工作,因为我的代码停止正常传递 "extraCapabilities"。

这是过去的工作方式:

require(RSelenium)
require(XML)
require(data.table)
source(file.path(find.package("RSelenium"), "examples/serverUtils/checkForServer.r"))
source(file.path(find.package("RSelenium"), "examples/serverUtils/startServer.r"))
checkForServer(); 
server<-startServer()

referencedirectory <- "d://temp"
fprof <- makeFirefoxProfile(list(browser.download.dir = referencedirectory,  browser.download.folderList = 2L, browser.download.manager.showWhenStarting = FALSE,
    browser.helperApps.neverAsk.saveToDisk="text/xml",browser.tabs.remote.autostart = FALSE,browser.tabs.remote.autostart.2 = FALSE,browser.tabs.remote.desktopbehavior = FALSE))

remDr <- remoteDriver(remoteServerAddr = "localhost", port = 4444, browserName = "firefox",extraCapabilities = fprof)
remDr$open()

现在它抛出一个错误:

Selenium message:Profile has been set on both the capabilities and these options, but they're different. Unable to determine which one you want to use.

Error:   Summary: UnknownError
     Detail: An unknown server-side error occurred while processing the command.
     class: java.lang.IllegalStateException
     Further Details: run errorDetails method

我试过另一种方法:

rD <- rsDriver(port = 4444L, browser = "firefox", version = "latest", geckover = "0.15.0", iedrver = NULL, phantomver = "2.1.1",
  verbose = TRUE, check = TRUE, extraCapabilities = fprof)

除了抱怨之外还会产生同样的错误(这些抱怨本身不会导致错误):

Selenium message:wrong number of arguments

如果删除了 extraCapabilities,上面的代码就会执行,但是如果您随后尝试:

rD <- rsDriver(port = 4446L, browser = "firefox", version = "latest", geckover = "0.15.0", iedrver = NULL, phantomver = "2.1.1",
  verbose = TRUE, check = TRUE)
remDr <- rD[["client"]]
fprof <- makeFirefoxProfile(list(browser.download.dir = "D:/temp"))
remDr <- remoteDriver(extraCapabilities = fprof)
remDr$open()

你在最后一行之后得到同样的错误。 rsDriver 打开浏览器,但该浏览器没有任何所需的属性。如果在尝试分配 remDr 并打开它之前关闭浏览器(而不关闭服务器),您仍然会得到相同的错误。

驱动的13、14、15版本和Server 3.1.0都试过了,结果一样。

我在 Java 中找到了引发错误的行,但我不知道如何传递不同于在后台自动生成的 Firefox 配置文件的方法。我已经尝试了 "Profile"/"requiredProfile"/"FirefoxProfile " 等的各种版本,但是这些都没有被识别为有效输入...我还看到了一些关于它可能是如何的讨论在 Java 中完成,但不在 R.

中完成

直到大约 36 小时前,该代码一直对我有用,从那以后我一直在努力寻找摆脱它的方法。我现在完全迷失了。

更新:设置对版本组合非常敏感。全新的 Selenium 服务器版本 (3.3.1) 适用于 Gecko 0.15.0 和 Firefox 52。其他一些组合可能有效,但大多数无效。

此外,在设置文件夹位置字符串时需要小心。在 R 中的大多数上下文中,正斜杠 / 是 OS 中性的,因此,我大部分时间都在 UNIX 和 Windows 中使用它。但是,当在 Windows 中设置 browser.download.dir 时,显然必须使用(转义的)反斜杠 \。否则目录分配看起来有效,但实际上无效。

最后,使用 rsDriver 的推荐方法有效并且使用已失效函数的方法也再次有效(checkForServer()startServer)。要吸取的教训:不要像我一样倒霉地选择更新 Selenium 代码的时机

这似乎是 geckodriver(0.15.0)/selenium(3.3.0) 的问题。我使用了以下内容:

library(RSelenium)
referencedirectory <- "c://temp"
fprof <- makeFirefoxProfile(list(browser.download.dir = referencedirectory,  browser.download.folderList = 2L, browser.download.manager.showWhenStarting = FALSE,
                                 browser.helperApps.neverAsk.saveToDisk="text/xml",browser.tabs.remote.autostart = FALSE,browser.tabs.remote.autostart.2 = FALSE,browser.tabs.remote.desktopbehavior = FALSE))
rD <- rsDriver(port = 4444L, browser = "firefox", version = "3.1.0", geckover = "0.14.0", iedrver = NULL, phantomver = "2.1.1",
               verbose = TRUE, check = TRUE, extraCapabilities = fprof)

似乎运行正常。如文档中所述,如果可能,我会建议将 Docker 图像用于 运行 Selenium 服务器,这将防止 browser/driver 版本不兼容的问题。

更新:

有更新版本的 selenium 服务器现在应该可以解决这个问题:

rD <- rsDriver(port = 4444L, browser = "firefox", version = "3.3.1", geckover = "0.15.0",
               verbose = TRUE, check = TRUE, extraCapabilities = fprof)

看来你真的不需要makefireprof。 代码确实很简单:

remDr=rsDriver(browser=browserName,extraCapabilities=list(acceptInsecureCerts=TRUE,acceptUntrustedCerts=TRUE))