简体   繁体   English

如何使用RSelenium上传文件?

[英]How to upload files with RSelenium?

I am trying to find out how to upload a file using R/RSelenium. 我试图找出如何使用R / RSelenium上传文件。 Informations: 信息:

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

I tried the top comment from this SO question: 我尝试了这个SO问题的最高评论:

How to upload file using Selenium WebDriver in Java 如何使用Java中的Selenium WebDriver上传文件

Example: 例:

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)

But I get the following error message: 但是我收到以下错误消息:

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

The folder used is mapped to the virtual machine. 使用的文件夹映射到虚拟机。 Autoit is out of the question since it only works on Windows. Autoit不可能的,因为它只适用于Windows。

Also tried upload_btn$sendKeysToElement(list(path)) which does not return an error, but it is not working either. 还尝试了upload_btn$sendKeysToElement(list(path)) ,它不会返回错误,但它也无法正常工作。

Any help is appreciated. 任何帮助表示赞赏。


Edit : 编辑

I think this is supposed to be working but I am seeing an error when viewing a screenshot: 我认为这应该有效但我在查看截图时看到错误:

  • Mounted my working folder to the default virtual machine as a shared folder and named it win_share 将我的工作文件夹作为共享文件夹安装到default虚拟机,并将其命名为win_share
  • Created a folder on default with sudo mkdir vm_share 使用sudo mkdir vm_share创建default文件夹
  • Mounted win_share to the folder vm_share with sudo mount -t vboxsf win_share vm_share . 安装win_share文件夹vm_sharesudo mount -t vboxsf win_share vm_share After this step I can successfully access my working folder on the virtual machine (checked by ssh into default ). 完成此步骤后,我可以成功访问虚拟机上的工作文件夹( default通过ssh检查)。
  • The path of vm_share folder is /home/docker/vm_share vm_share文件夹的路径是/home/docker/vm_share

After all of these executing this script it doesn't work. 在所有这些执行此脚本后,它不起作用。 (took John's example) (以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()

Screenshot : 截图

错误

The sendKeysToElement method expects a list. sendKeysToElement方法需要一个列表。 The path needs to be passed as a list: 路径需要作为列表传递:

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

See also the demo in the RSelenium package itself https://github.com/ropensci/RSelenium/blob/master/demo/selFileUpload.R and OpenFileDialog in R Selenium 另请参阅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