繁体   English   中英

如何使用RSelenium上传文件?

[英]How to upload files with RSelenium?

我试图找出如何使用R / RSelenium上传文件。 信息:

  • 操作系统:Win 8.1,RSelenium_1.7.1,带有docker镜像(linux,standalone-chrome 3.2.0)。

我尝试了这个SO问题的最高评论:

如何使用Java中的Selenium WebDriver上传文件

例:

url <- "https://www.freepdfconvert.com/pdf-word"
path <- "C:/path_to_folder/filename.pdf"

remDr$navigate(url)

upload_btn <- remDr$findElement(using = "id", "clientUpload")
upload_btn$sendKeysToElement(path)

但是我收到以下错误消息:

Selenium message:java.lang.String cannot be cast to java.util.List

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

使用的文件夹映射到虚拟机。 Autoit不可能的,因为它只适用于Windows。

还尝试了upload_btn$sendKeysToElement(list(path)) ,它不会返回错误,但它也无法正常工作。

任何帮助表示赞赏。


编辑

我认为这应该有效但我在查看截图时看到错误:

  • 将我的工作文件夹作为共享文件夹安装到default虚拟机,并将其命名为win_share
  • 使用sudo mkdir vm_share创建default文件夹
  • 安装win_share文件夹vm_sharesudo mount -t vboxsf win_share vm_share 完成此步骤后,我可以成功访问虚拟机上的工作文件夹( default通过ssh检查)。
  • vm_share文件夹的路径是/home/docker/vm_share

在所有这些执行此脚本后,它不起作用。 (以John为例)

library(RSelenium)

remDr <- remoteDriver(remoteServerAddr = "192.168.99.100" 
                                            , port = 4445L
                                            , browserName = "chrome"
)
remDr$open()
remDr$navigate("https://gallery.shinyapps.io/uploadfile")
webElem <- remDr$findElement("id", "file1")

# create a dummy csv 
x <- data.frame(a = 1:4, b = 5:8, c = letters[1:4])
write.csv(x, file = "testcsv.csv", row.names = FALSE)

# post the file to the app
path <- "/home/docker/vm_share/testcsv.csv"
webElem$sendKeysToElement(list(path))

remDr$close()
remDr$closeServer()

截图

错误

sendKeysToElement方法需要一个列表。 路径需要作为列表传递:

library(RSelenium)
appURL <- "https://www.freepdfconvert.com/pdf-word"
# create sample pdf
tfile <- tempfile("sample", fileext = ".pdf")
pdf(tfile,width=7,height=5)
x=rnorm(100)
y=rnorm(100,5,1)
plot(x,lty=2,lwd=2,col="red")
lines(y,lty=3,col="green")
dev.off()

rD <- rsDriver()
remDr <- rD$client
remDr$navigate(appURL)

upload_btn <- remDr$findElement(using = "id", "clientUpload")
upload_btn$sendKeysToElement(list(tfile))

......
# cleanup when finished
rm(rD)
gc()

另请参阅RSelenium软件包本身的演示https://github.com/ropensci/RSelenium/blob/master/demo/selFileUpload.RR Selenium中的OpenFileDialog

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM