如何使用 RSelenium 从浏览器对话框上传文件

How to upload file from a browser dialog using RSelenium

我有以下 URL 我想要 post 的形式:

https://cluspro.bu.edu/home.php

这个脚本可以完成这项工作(感谢 DPH):

library(RSelenium)

# Follow  instruction here
# https://rpubs.com/grahamplace/rseleniumonmac
# Step 1: cd /path_to_r_code/
# Step 2: ./geckodriver     #downloadable here: https://github.com/mozilla/geckodriver/releases/tag/v0.30.0
# Step 3: java -jar selenium-server-standalone.jar -port 5556
# Step 4: run this script

webpage <- 'https://cluspro.bu.edu/home.php'
my_jobname <- "test"
receptor_pdb <- "/path/to/my_file/2fyl.pdb"
ligand_pdb <- "/path/to/my_file/144_from_2yrq.pdb" # tried with double slash but don't work
browser <- remoteDriver(port = 5556)
browser$open()
browser$navigate(webpage)

# This works
clk <- browser$findElement(using = "link text", "Use the server without the benefits of your own account")
clk$clickElement()

# This works
jbn <- browser$findElement(using = "name", "jobname")
jbn$sendKeysToElement(list(my_jobname))

# This works
svr <- browser$findElement(using = "name", "server")
svr$sendKeysToElement(list("gpu"))

# This doesn't work
receptor <- browser$findElement(using = "id", "showrecfile")
receptor$clickElement()
receptor$sendKeysToElement(list(receptor_pdb))

# # This doesn't work as well
# ligand <- browser$findElement(using = "id", "showligfile")
# ligand$sendKeysToElement(list(ligand_pdb))
# 
# This works
agree <- browser$findElement(using = "name", "noncommercial")
agree$clickElement()
# 
# This works
# dock <- browser$findElement(using = "name", "action")
# dock$clickElement()

除了要求浏览器上传到受体的页面:

对应的HTML是这样的:

  <div>
    <span id="showrecpdb" class="link">Use PDB ID</span><span id="showrecfile" class="link">Upload PDB</span>
  </div>

上面这部分代码:

receptor <- browser$findElement(using = "id", "showrecfile")
receptor$clickElement()
receptor$sendKeysToElement(list(receptor_pdb))

只激活了如下浏览器按钮,但没有上传或选择文件。

如果有效,它将看起来像这样:

如何启用它?

我正在使用 MacOS.X

可以下载PDB文件here (receptor) and here (ligand)

RSelenium 软件包可以从 R 远程控制常见的 Web 浏览器。以下代码在 mozilla firefox 中打开一个远程浏览器会话(chrome 应该也能正常工作)并在登录屏幕上工作 - 使用正确的 usr 和密码将打开下一个页面。由于我无法访问已关闭的部分,因此在单击登录屏幕后我无法尝试也无法调试我的代码,所以我只显示了一个上传:

library(RSelenium)
webpage <- 'https://cluspro.bu.edu/home.php'
# set up and start remote controlle browser
driver <- RSelenium::rsDriver(browser = 'firefox', port = 4818L)
remDr <- driver[['client']]
# navigate to the page
remDr$navigate(webpage)
# find elements by id and populate/click them
usr <- remDr$findElement(using = "id", "username")
usr$sendKeysToElement(list("your user name"))
psw <- remDr$findElement(using = "id", "password")
psw$sendKeysToElement(list("your password"))
clk <- remDr$findElement(using = "id", "submit")
clk$clickElement()
# next page will open (you already know how to fill out the textboxes so here goes one upload
upl <- remDr$findElement(using = "id", "showrecpdb")
# you need a path with these double backslashes
upl$sendKeysToElement(list("C:\...\...\your_file.something"))
# session end
remDr$close()

这是你想要的吗:

# This for upload 1 link
receptor <- browser$findElement(using = "id", "showrecfile")
receptor$clickElement()

# This is the upload 1 file element
rec <- browser$findElement(using = "id", "rec")
rec$sendKeysToElement(list(receptor_pdb))

# This for upload 2
ligand <- browser$findElement(using = "id", "showligfile")
ligand$clickElement()

#This is the upload 2 file element
lig <- browser$findElement(using = "id", "lig")
lig$sendKeysToElement(list(ligand_pdb))