码头上的RSelenium:文件在哪里下载? [英] RSelenium on docker: where are files downloaded?
问题描述
require(RSelenium)
如果length(system(docker ps -l,intern = TRUE))< 2)
try({system(docker run -d -p 4445:4444 selenium / standalone-firefox:2.53.0) })
它可以连接到任何url并导航。但是当我点击一个按钮来下载文件时,它有时将它保存(部分地保存为 xxxxxxx.csv.part
)到 / tmp / mozilla_mozillaUser0
,有时候...没有地方,或者可能找不到另一个位置...
有没有原因? p>
此外,我也尝试使用
makeFirefoxProfile(list(browser .download.dir =D:/ temp))
但它返回一个奇怪的错误
文件中的错误(tmpfile,rb):无法打开连接
另外:警告消息:
1:运行命令'zip-r9XjqC:\Users\rocks\AppData\Local\Temp\RtmpoPhjUb\file31076202d4f.zipC:\Users\rocks\AppData \Local\Temp\RtmpoPhjUb / firefoxprofile / prefs.js'状态127
2:在文件(tmpfile,rb):
无法打开文件'C:\Users\\ \\rocks\AppData\Local\Temp\\ RtmpoPhjUb\file31076202d4f.zip':没有这样的文件或目录
我可以理解为什么这个desn' t工作,因为所有链接都在Windows中,但是我的selenium在docker容器或Ubuntu中运行。
我的安装程序:R在Windows上运行,我有一个docker包含硒服务器的Ubuntu。
编辑: 原来我的问题是因为主机上没有安装firefox em>(理论上不需要)。
我通过使用(而不是调用 makeFirefoxProfile
)来修复问题:
fprof =结构(列表(firefox_profile = UEsDBBQAAAAIANJiVEobimJN8QAAABkCAAAIAAAAcHJlZnMuanOFkT9PwzAQxXckvkOUCSQnlliZkDqygcSITHyJTW3fyXdNIj49btqBoLTd7t57v / OfOzDkT8rQP9RfGafStRanFNDY1vpcq6rWDiNohnDM6t3Z5frx + f7uGt5jsJBfPUuZ8nQrHU0yQxHY4fThIL2JyeLTUNjeBIYLvINAkF + IuE0wlor3LZsR3nHneX + 8fjwE8VSG6bn58aQMUfCdEY9J / + + tPiabDWA5hBrs2LxmRJ8xrqeNRCcyyFD6Wl2lKQ3UuvwkGVf3Nk 1VtQBOYjihFIxPqlrlYO4grNExlc / jZsOZt8XIGzJ2AtKwZDBx2ewvUEsBAj8AFAAAAAgA0mJUShuKYk3xAAAAGQIAAAgAJAAAAAAAAAAgAAAAAAAAAHByZWZzLmpzCgAgAAAAAAABABgAzaOo9TCL0gHdkgMtLYvSAd2SAy0ti9IBUEsFBgAAAAABAAEAWgAAABcBAAAAAA ==),.Names = firefox_profile)
remDr< - remoteDriver (extraCapabilities = ePrefs,port = 4445)
容器是运行它的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)。
然后,我们需要在firefox配置文件中添加必要的信息:
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压缩,应用程序/ x压缩,应用程序/ 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 [ (@href,'zip')])
firstzip $ clickElement()
我们可以检查下载是否在HOST机器上:
> list.files(/ home / john / test /)
[1]peace.zip
I am using Selenium using a docker image:
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")})
It works, I can connect to any url and navigate. However when I click a button to download a file, it sometimes saves it (partially, saved as xxxxxxx.csv.part
) to /tmp/mozilla_mozillaUser0
, and sometimes to ... nowhere, or maybe another location I cannot find...
Is there a reason for that?
Also I tried to open the driver using
makeFirefoxProfile(list(browser.download.dir = "D:/temp"))
but it returns a weird error
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
where I can understand why this desn't work given that all links are in windows but my selenium runs in a docker container or Ubuntu.
My Setup: R running on Windows, and I have a docker image of Ubuntu that contains the selenium server.
EDIT: turns out my issue was because firefox was not installed on the host machine (which is not needed in theory).
I fixed the issue by using (instead of invoking makeFirefoxProfile
):
fprof = structure(list(firefox_profile = "UEsDBBQAAAAIANJiVEobimJN8QAAABkCAAAIAAAAcHJlZnMuanOFkT9PwzAQxXckvkOUCSQnlliZkDqygcSITHyJTW3fyXdNIj49btqBoLTd7t57v/OfOzDkT8rQP9RfGafStRanFNDY1vpcq6rWDiNohnDM6t3Z5frx+f7uGt5jsJBfPUuZ8nQrHU0yQxHY4fThIL2JyeLTUNjeBIYLvINAkF+IuE0wlor3LZsR3nHneX+8fjwE8VSG6bn58aQMUfCdEY9J/+tPiabDWA5hBrs2LxmRJ8xrqeNRCcyyFD6Wl2lKQ3UuvwkGVf3Nk+1VtQBOYjihFIxPqlrlYO4grNExlc/jZsOZt8XIGzJ2AtKwZDBx2ewvUEsBAj8AFAAAAAgA0mJUShuKYk3xAAAAGQIAAAgAJAAAAAAAAAAgAAAAAAAAAHByZWZzLmpzCgAgAAAAAAABABgAzaOo9TCL0gHdkgMtLYvSAd2SAy0ti9IBUEsFBgAAAAABAAEAWgAAABcBAAAAAA=="), .Names = "firefox_profile")
remDr <- remoteDriver(extraCapabilities = ePrefs, port = 4445)
The docker container is a separate entity to the HOST which is running it. You need to map a directory on the HOST to a directory on the container you download files to:
You can do this when starting your container:
docker run -d -p 4445:4444 -p 5901:5900 -v /home/john/test:/home/seluser/Downloads selenium/standalone-firefox-debug:2.53.1
Here (i am running docker on linux) I have mapped a directory on my linux HOST (/home/john/test) to a directory on the container (/home/seluser/Downloads).
We then need to add the necessary information to the firefox profile:
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()
We can check if the download is on the HOST machine:
> list.files("/home/john/test/")
[1] "peace.zip"
这篇关于码头上的RSelenium:文件在哪里下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!