我正在使用Selenium使用docker镜像:
require(RSelenium)
if (length(system("docker ps -l",intern = TRUE))<2)
try({system("docker run -d -p 4445:4444 selenium/standalone-firefox:2.53.0")})
它工作,我可以连接到任何网址并导航.但是,当我单击按钮下载文件时,它有时将其保存(部分保存为xxxxxxx.csv.part)到/ tmp / mozilla_mozillaUser0,有时保存到…无处,或者可能是其他位置我找不到…
这有什么理由吗?
我还尝试使用打开驱动程序
makeFirefoxProfile(list(browser.download.dir = "D:/temp"))
但它返回一个奇怪的错误
Error in file(tmpfile,"rb") : cannot open the connection
In addition: Warning messages:
1: running command '"zip" -r9Xjq "C:\Users\rocks\AppData\Local\Temp\RtmpoPhjUb\file31076202d4f.zip" "C:\Users\rocks\AppData\Local\Temp\RtmpoPhjUb/firefoxprofile/prefs.js" ' had status 127
2: In file(tmpfile,"rb") :
cannot open file 'C:\Users\rocks\AppData\Local\Temp\RtmpoPhjUb\file31076202d4f.zip': No such file or directory
鉴于所有链接都在windows中但我的selenium在docker容器或Ubuntu中运行,我可以理解为什么这不起作用.
我的设置:R在Windows上运行,我有一个包含selenium服务器的Ubuntu的docker镜像.
编辑:结果我的问题是因为主机上没有安装firefox(理论上不需要).
我通过使用(而不是调用makeFirefoxProfile)修复了这个问题:
fprof = structure(list(firefox_profile = "UEsDBBQAAAAIANJiVEobimJN8QAAABkCAAAIAAAAcHJlZnMuanOFkT9PwzAQxXckvkOUCSQnlliZkDqygcSITHyJTW3fyXdNIj49btqBoLTd7t57v/OfOzDkT8rQP9RfGafStRanFNDY1vpcq6rWDiNohnDM6t3Z5frx+f7uGt5jsJBfPUuZ8nQrHU0yQxHY4fThIL2JyeLTUNjeBIYLvINAkF+IuE0wlor3LZsR3nHneX+8fjwE8VSG6bn58aQMUfCdEY9J/+tPiabDWA5hBrs2LxmRJ8xrqeNRCcyyFD6Wl2lKQ3UuvwkGVf3Nk+1VtQBOYjihFIxPqlrlYO4grNExlc/jZsOZt8XIGzJ2AtKwZDBx2ewvUEsBAj8AFAAAAAgA0mJUShuKYk3xAAAAGQIAAAgAJAAAAAAAAAAgAAAAAAAAAHByZWZzLmpzCgAgAAAAAAABABgAzaOo9TCL0gHdkgMtLYvSAd2SAy0ti9IBUEsFBgAAAAABAAEAWgAAABcBAAAAAA=="),.Names = "firefox_profile")
remDr <- remoteDriver(extraCapabilities = ePrefs,port = 4445)
最佳答案
docker容器是运行它的HOST的独立实体.您需要将HOST上的目录映射到您下载文件的容器上的目录:
您可以在启动容器时执行此操作:
docker run -d -p 4445:4444 -p 5901:5900 -v /home/john/test:/home/seluser/Downloads selenium/standalone-firefox-debug:2.53.1
这里(我在Linux上运行docker)我已经将我的linux HOST(/ home / john / test)上的目录映射到容器上的目录(/ home / seluser / Downloads).
library(RSelenium)
ePrefs <- makeFirefoxProfile(
list(
browser.download.dir = "/home/seluser/Downloads","browser.download.folderList" = 2L,"browser.download.manager.showWhenStarting" = FALSE,"browser.helperApps.neverAsk.saveToDisk" = "multipart/x-zip,application/zip,application/x-zip-compressed,application/x-compressed,application/msword,application/csv,text/csv,image/png,image/jpeg,application/pdf,text/html,text/plain,application/excel,application/vnd.ms-excel,application/x-excel,application/x-msexcel,application/octet-stream"))
remDr <- remoteDriver(extraCapabilities = ePrefs,port = 4445)
remDr$open()
remDr$navigate("http://www.colorado.edu/conflict/peace/download/")
firstzip <- remDr$findElement("xpath","//a[contains(@href,'zip')]")
firstzip$clickElement()
我们可以检查下载是否在HOST机器上:
> list.files("/home/john/test/")
[1] "peace.zip"