如何使用RSelenium上传文件?

GyD*_*GyD 4 selenium file-upload r rselenium

我正在尝试了解如何使用 R/RSelenium 上传文件。信息:

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

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

如何在 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)
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误消息:

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
Run Code Online (Sandbox Code Playgroud)

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

还尝试了upload_btn$sendKeysToElement(list(path))不返回错误的方法,但它也不起作用。

任何帮助表示赞赏。


编辑

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

  • 将我的工作文件夹default作为共享文件夹挂载到虚拟机并命名win_share
  • default创建了一个文件夹sudo mkdir vm_share
  • 安装win_sharevm_share带有sudo mount -t vboxsf win_share vm_share. 完成此步骤后,我可以成功访问虚拟机上的工作文件夹(通过sshinto进行检查default)。
  • vm_share文件夹的路径是/home/docker/vm_share

在执行所有这些之后,该脚本不起作用。(以约翰为例)

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()
Run Code Online (Sandbox Code Playgroud)

截屏

错误

jdh*_*son 5

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()
Run Code Online (Sandbox Code Playgroud)

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