[英]How to upload files with RSelenium?
I am trying to find out how to upload a file using R/RSelenium. 我试图找出如何使用R / RSelenium上传文件。 Informations:
信息:
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: 我认为这应该有效但我在查看截图时看到错误:
default
virtual machine as a shared folder and named it win_share
default
虚拟机,并将其命名为win_share
default
with sudo mkdir vm_share
sudo mkdir vm_share
创建default
文件夹 win_share
to the folder vm_share
with sudo mount -t vboxsf win_share vm_share
. win_share
文件夹vm_share
与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
). default
通过ssh
检查)。 /home/docker/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.R和R Selenium中的OpenFileDialog
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.