[英]How to upload files with RSelenium?
我試圖找出如何使用R / RSelenium上傳文件。 信息:
我嘗試了這個SO問題的最高評論:
如何使用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)
但是我收到以下錯誤消息:
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
使用的文件夾映射到虛擬機。 Autoit是不可能的,因為它只適用於Windows。
還嘗試了upload_btn$sendKeysToElement(list(path))
,它不會返回錯誤,但它也無法正常工作。
任何幫助表示贊賞。
編輯 :
我認為這應該有效但我在查看截圖時看到錯誤:
default
虛擬機,並將其命名為win_share
sudo mkdir vm_share
創建default
文件夾 win_share
文件夾vm_share
與sudo mount -t vboxsf win_share vm_share
。 完成此步驟后,我可以成功訪問虛擬機上的工作文件夾( default
通過ssh
檢查)。 /home/docker/vm_share
在所有這些執行此腳本后,它不起作用。 (以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()
截圖 :
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()
另請參閱RSelenium
軟件包本身的演示https://github.com/ropensci/RSelenium/blob/master/demo/selFileUpload.R和R Selenium中的OpenFileDialog
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.