使用 Rselenium 从下拉菜单中获取值
get the values from drop down menu using Rselenium
我需要从网站获取数据,但从下拉菜单获取值的代码无法正常工作,此代码适用于其他网站但不适用于此网站。不知道为什么。
下面是我试图抓取的代码和网站。我是 RSelenium,如果可能的话,我想使用这个包来完成这个任务。
library(tidyverse)
library(RSelenium)
rD <- try(rsDriver(browser = "chrome"
,verbose = TRUE
,chromever = "91.0.4472.101"))
remDr <- try(rD[["client"]])
remDr$navigate("https://assist.org/")
Sys.sleep(2)
DropDownOptions <- XML::htmlParse(remDr$getPageSource()[[1]]) %>%
XML::xmlRoot(.) %>%
XML::getNodeSet('//select[@id="academicYear"]/option')
DropDownOptions <- data.frame(ID = sapply(DropDownOptions, XML::xmlGetAttr, "value")
,Name = sapply(DropDownOptions, XML::xmlValue))
DropDownOptions
data frame with 0 columns and 0 rows
```
所以你想要select学年,
library(RSelenium)
driver = rsDriver(port = 4741L, browser = c("firefox"))
remDr <- driver[["client"]]
remDr$navigate("https://assist.org/")
#Select the drop down menu on Academic Year
remDr$findElement(using = "xpath",'//*[@id="transfer-agreement-search"]/div[1]/ng-select/div') -> dropdownmenu
dropdownmenu$clickElement()
# Select the Academic year 2018-2019 (fullXpath)
remDr$findElement(using = "xpath",'/html/body/app-root/div[2]/app-home-component/section[2]/app-form-container/div/div[1]/app-transfer-agreements-form/div/div[2]/form/div[1]/ng-select/ng-dropdown-panel/div/div[2]/div[4]') -> acdyear
acdyear$clickElement()
我需要从网站获取数据,但从下拉菜单获取值的代码无法正常工作,此代码适用于其他网站但不适用于此网站。不知道为什么。
下面是我试图抓取的代码和网站。我是 RSelenium,如果可能的话,我想使用这个包来完成这个任务。
library(tidyverse)
library(RSelenium)
rD <- try(rsDriver(browser = "chrome"
,verbose = TRUE
,chromever = "91.0.4472.101"))
remDr <- try(rD[["client"]])
remDr$navigate("https://assist.org/")
Sys.sleep(2)
DropDownOptions <- XML::htmlParse(remDr$getPageSource()[[1]]) %>%
XML::xmlRoot(.) %>%
XML::getNodeSet('//select[@id="academicYear"]/option')
DropDownOptions <- data.frame(ID = sapply(DropDownOptions, XML::xmlGetAttr, "value")
,Name = sapply(DropDownOptions, XML::xmlValue))
DropDownOptions
data frame with 0 columns and 0 rows
```
所以你想要select学年,
library(RSelenium)
driver = rsDriver(port = 4741L, browser = c("firefox"))
remDr <- driver[["client"]]
remDr$navigate("https://assist.org/")
#Select the drop down menu on Academic Year
remDr$findElement(using = "xpath",'//*[@id="transfer-agreement-search"]/div[1]/ng-select/div') -> dropdownmenu
dropdownmenu$clickElement()
# Select the Academic year 2018-2019 (fullXpath)
remDr$findElement(using = "xpath",'/html/body/app-root/div[2]/app-home-component/section[2]/app-form-container/div/div[1]/app-transfer-agreements-form/div/div[2]/form/div[1]/ng-select/ng-dropdown-panel/div/div[2]/div[4]') -> acdyear
acdyear$clickElement()