4

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:

Error

Community
  • 1
  • 1
GyD
  • 3,902
  • 2
  • 18
  • 28
  • You will need to give a folder/path on the docker container. This in turn would be mapped to a corresponding folder/path on the Host. If you are passing a folder referring to the Host as your `path` this will not work. – jdharrison Apr 18 '17 at 14:41
  • 1
    It looks like you followed http://stackoverflow.com/questions/30864466/whats-the-best-way-to-share-files-from-windows-to-boot2docker-vm to create your shared folder. You would need to reference this when starting your selenium docker image using for example `docker run -d -v vm_share:/home/docker/vm_share -p 4445:4444 selenium/standalone-chrome`. This should then create a mapping between the boot2docker vm folder and the container folder `/home/docker/vm_share`. The vm folder itself already mapped to your windows host. – jdharrison Apr 19 '17 at 18:56
  • awesome work, thank you for taking your time with this! – GyD Apr 20 '17 at 09:47

1 Answers1

5

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

Community
  • 1
  • 1
jdharrison
  • 30,085
  • 4
  • 77
  • 89
  • Hi, John, thank you for your answer! I did try to pass path as a list and it doesn't return an error, however the file doesn't get uploaded, Either I get an error or I just can't click the submit button afterwards. Checked with this code as well (https://github.com/ropensci/RSelenium/blob/master/demo/selFileUpload.R), but it's not working so I guess I should check with other selenium versions / browsers. (I am using selenium-server-standalone-3.3.1, with selenium/standalone-chrome:latest, also tried firefox:3.2.0 and chrome:3.2.0) – GyD Apr 15 '17 at 09:35
  • I have updated the example which runs with latest chrome and selenium server – jdharrison Apr 15 '17 at 18:03
  • You may also need to pass the path OS dependent e.g. `C:\\path_to_folder\\filename.pdf` for windows. – jdharrison Apr 15 '17 at 18:10
  • Thanks, it should work, but I am curious why my solution is not working (see edit section of my question). I would appreciate it if you could take a look at it. – GyD Apr 19 '17 at 13:16