How to upload file from a browser dialog using RSelenium

Viewed 179

I have the following URL which I want to post the form:

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

This script does the job (thanks to 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()

Except on the page where it ask for browser upload to receptor:

enter image description here

The corresponding HTML is this:

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

This part of the code above:

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

only activated the browser button like below but no file uploaded or selected.

enter image description here

If it works, it'll look something like this:

enter image description here

How can I enable it?

I'm using MacOS.X

The PDB file can be downloaded here (receptor) and here (ligand).

2 Answers

The RSelenium package enables to remote control the comon web browsers from R. The following code opens a remote browser session in mozilla firefox(chrome should work as well) and works the login screen - with the correct usr and pw the next page will open. As I do not have access to the closed part I can not try nor debug my code after the login screen click so I just showed one upload:

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()

Is this what you are after:

# 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))
Related