Search code examples
htmlrseleniumselenium-firefoxdriverrselenium

How to upload file from a browser dialog using RSelenium


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


Solution

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