RSelenium 下拉菜单选择
RSelenium Drop-Down Menu Selection
我正在尝试使用 RSelenium
到 select 下拉菜单中的选项,并且正在使用 XPath 来定位我感兴趣的元素。
网站是: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,我正在尝试以 PMID
格式保存结果。我的代码 select 是格式框,但是当我希望它单击 PMID
选项时,它并没有这样做。任何帮助将不胜感激。下面是我的代码:
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()
通过
得到当前页所有文章的PMID
- 使用
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"
与第二页类似,
'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"
- 使用
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()
我正在尝试使用 RSelenium
到 select 下拉菜单中的选项,并且正在使用 XPath 来定位我感兴趣的元素。
网站是: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,我正在尝试以 PMID
格式保存结果。我的代码 select 是格式框,但是当我希望它单击 PMID
选项时,它并没有这样做。任何帮助将不胜感激。下面是我的代码:
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()
通过
得到当前页所有文章的PMID
- 使用
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"
与第二页类似,
'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"
- 使用
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()