简体   繁体   中英

How to upload files with RSelenium?

I am trying to find out how to upload a file using R/RSelenium. Informations:

  • OS: Win 8.1, RSelenium_1.7.1, with a docker image (linux, standalone-chrome 3.2.0).

I tried the top comment from this SO question:

How to upload file using Selenium WebDriver in Java

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.

Also tried upload_btn$sendKeysToElement(list(path)) which does not return an error, but it is not working either.

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
  • Created a folder on default with sudo mkdir vm_share
  • Mounted win_share to the folder vm_share with sudo 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 ).
  • The path of vm_share folder is /home/docker/vm_share

After all of these executing this script it doesn't work. (took John's example)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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