簡體   English   中英

如何使用 RSelenium 從瀏覽器對話框上傳文件

[英]How to upload file from a browser dialog using RSelenium

我有以下 URL,我想發布表單:

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 文件可以在此處(受體)此處(配體)下載

RSelenium包允許從R遠程控制RSelenium的 Web 瀏覽器。 下面的代碼在mozilla firefox打開一個遠程瀏覽器會話( chrome應該也能工作)並運行登錄屏幕 - 使用正確的 usr 和 pw 將打開下一頁。 由於我無法訪問已關閉的部分,因此我無法在登錄屏幕點擊后嘗試或調試我的代碼,所以我只展示了一個上傳:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM