docker上的RSelenium:文件在哪里下载? [英] RSelenium on docker: where are files downloaded?

查看:17
本文介绍了docker上的RSelenium:文件在哪里下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Selenium 使用 docker 映像:

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")})

它有效,我可以连接到任何 url 并导航.但是,当我单击按钮下载文件时,它有时会将其保存(部分保存为 xxxxxxx.csv.part)到 /tmp/mozilla_mozillaUser0,有时会保存到 ... 无处可去,或者可能是我找不到的另一个位置...

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...

有什么原因吗?

我也尝试使用

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
ocksAppDataLocalTempRtmpoPhjUbfile31076202d4f.zip" "C:Users
ocksAppDataLocalTempRtmpoPhjUb/firefoxprofile/prefs.js" ' had status 127 
2: In file(tmpfile, "rb") :
  cannot open file 'C:Users
ocksAppDataLocalTempRtmpoPhjUbfile31076202d4f.zip': No such file or directory

鉴于所有链接都在 Windows 中,但我的 selenium 在 docker 容器或 Ubuntu 中运行,我可以理解为什么这不起作用.

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.

我的设置:R 在 Windows 上运行,我有一个 Ubuntu 的 docker 映像,其中包含 selenium 服务器.

My Setup: R running on Windows, and I have a docker image of Ubuntu that contains the selenium server.

原来我的问题是因为主机上没有安装 firefox(理论上不需要).我通过使用(而不是调用 makeFirefoxProfile)解决了这个问题:

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 =结构(列表(firefox_profile = UEsDBBQAAAAIANJiVEobimJN8QAAABkCAAAIAAAAcHJlZnMuanOFkT9PwzAQxXckvkOUCSQnlliZkDqygcSITHyJTW3fyXdNIj49btqBoLTd7t57v/OfOzDkT8rQP9RfGafStRanFNDY1vpcq6rWDiNohnDM6t3Z5frx + f7uGt5jsJBfPUuZ8nQrHU0yQxHY4fThIL2JyeLTUNjeBIYLvINAkF + IuE0wlor3LZsR3nHneX + 8fjwE8VSG6bn58aQMUfCdEY9J/+ + tPiabDWA5hBrs2LxmRJ8xrqeNRCcyyFD6Wl2lKQ3UuvwkGVf3Nk 1VtQBOYjihFIxPqlrlYO4grNExlc/jZsOZt8XIGzJ2AtKwZDBx2ewvUEsBAj8AFAAAAAgA0mJUShuKYk3xAAAAGQIAAAgAJAAAAAAAAAAgAAAAAAAAAHByZWZzLmpzCgAgAAAAAAABABgAzaOo9TCL0gHdkgMtLYvSAd2SAy0ti9IBUEsFBgAAAAABAAEAWgAAABcBAAAAAA =="),.Names = firefox_profile")remDr <- remoteDriver(extraCapabilities = ePrefs,端口 = 4445)

推荐答案

docker 容器对于运行它的 HOST 来说是一个独立的实体.您需要将 HOST 上的目录映射到您将文件下载到的容器上的目录:

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

在这里(我在 linux 上运行 docker)我已将我的 linux HOST (/home/john/test) 上的一个目录映射到容器上的一个目录 (/home/seluser/Downloads).

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).

然后我们需要将必要的信息添加到 firefox 配置文件中:

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"

这篇关于docker上的RSelenium:文件在哪里下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆