Search code examples
rrselenium

RSelenium Drop-Down Menu Selection


I'm trying to use RSelenium to select an option from a drop-down menu, and am using XPath to locate the element I'm interested in.

The website is: https://pubmed.ncbi.nlm.nih.gov/?term=%282019-03-01%5BDate+-+Publication%5D+%3A+2019-03-31%5BDate+-+Publication%5D%29+AND+%28Hearing+Loss%5BMeSH+Terms%5D%29&size=200 and I'm trying to save the results in PMID format. My code selects the Format box, but when I want it to click the PMID option, it doesn't do so. Any help would be greatly appreciated. Below is my code:

webElem <- remDr$findElement(using = "css selector", "#save-results-panel-trigger")

webElem$sendKeysToElement(list(key = "enter"))

webElem <- remDr$findElement(using = "xpath", "//*[(@id = 'save-action-format')]")

webElem$clickElement()

remDr$findElement(using = "xpath","//*[(@value='pmid')]")$clickElement()

Solution

  • Get PMID of all articles of the current page through,

    1. Using rvest
    'https://pubmed.ncbi.nlm.nih.gov/?term=%282019-03-01%5BDate+-+Publication%5D+%3A+2019-03-31%5BDate+-+Publication%5D%29+AND+%28Hearing+Loss%5BMeSH+Terms%5D%29&size=200' %>% 
      read_html() %>% html_nodes('.docsum-pmid') %>% html_text()
    
    [1] "30842313" "30916228" "30920067" "30782832" "30471777" "30905565" "30892661" "30847886" "30954364" "30639959" "29555078" "31184964" "30634102" "30632288"
    

    Similarly from second page,

        'https://pubmed.ncbi.nlm.nih.gov/?term=%282019-03-01%5BDate+-+Publication%5D+%3A+2019-03-31%5BDate+-+Publication%5D%29+AND+%28Hearing+Loss%5BMeSH+Terms%5D%29&size=200&page=2' %>% 
          read_html() %>% html_nodes('.docsum-pmid') %>% html_text()
         [1] "30338413" "30833669" "30909120" "30623325" "30894283" "30324407" "30740830" "30682699" "30287322" "30929520" "30850599" "30907643" "30887954" "29927780"
    
    1. Using RSelenium
        library(RSelenium)
        #open browser 
        driver = rsDriver(
             port = 4847L,
               browser = c("firefox"))
        
        remDr <- driver[["client"]]
        
        url = 'https://pubmed.ncbi.nlm.nih.gov/?term=%282019-03-01%5BDate+-+Publication%5D+%3A+2019-03-31%5BDate+-+Publication%5D%29+AND+%28Hearing+Loss%5BMeSH+Terms%5D%29&size=200'
        remDr$navigate(url)
    
        #click on SAVE 
        remDr$findElement(using = "xpath",'//*[@id="save-results-panel-trigger"]')$clickElement()
        
        #click of selection
        address_element <- remDr$findElement("xpath", '//*[@id="save-action-selection"]')
        address_element$sendKeysToElement(list("all-results"))
        
        #click of format 
        address_element <- remDr$findElement("xpath", '//*[@id="save-action-format"]')
        address_element$sendKeysToElement(list("pmid"))
        
        #finally click on Create file
        remDr$findElement(using = "xpath",'//*[@id="save-action-panel-form"]/div[3]/button[1]')$clickElement()
        
        #close browser
        remDr$close()