RSelenium 无法在带有下拉菜单的页面上找到元素
RSelenium fails to find element on page with dropdowns
我正在尝试从 table 中抓取一些使用下拉菜单的数据。 table 是“GIC 选项”。 “Term”为“Mid-Term”,“Interest Type”为“Compound”。
查看 SO 答案 ,我试过:
library(RSelenium)
remDr <- remoteDriver(port = 4445L)
remDr$navigate("https://www.nbc.ca/personal/savings-investments/gic/non-redeemable.html")
webElem <- remDr$findElement(using = "xpath", '//*[@class="ft2-dropdown-list-element"]/option[@value="Mid-Term"]')
和一个css版本
webElem <- remDr$findElement(using = "css", "#ft2-filterDropdownBtnID option[value='Mid-Term']")
两者都导致 Selenium message:Unable to locate element
如何抓取此 table 中的数据?
下面是我的解决方案。
library(RSelenium)
driver <- rsDriver(browser=c("firefox"),port = 4445L)
remote_driver <- driver[["client"]]
remote_driver$navigate("https://www.nbc.ca/personal/savings-investments/gic/non-redeemable.html")
#Active the box by a click
remote_driver$findElement(using = "css selector", '.ft2-dropdown-btn-label')$clickElement()
#Now you can choose what you need
remote_driver$findElement(using = "xpath", '//*[@id="ft2"]/div[2]/div/ul/li[3]/a')$clickElement()
#All
//*[@id="ft2"]/div[2]/div/ul/li[1]/a
#Short term
//*[@id="ft2"]/div[2]/div/ul/li[2]/a
#Mid term
//*[@id="ft2"]/div[2]/div/ul/li[3]/a
#Long term
//*[@id="ft2"]/div[2]/div/ul/li[4]/a
#To scrape the value of the table, below a possible solution.
webElem <- remote_driver$findElement(using = "css selector", 'div.table-wrapper:nth-child(4) > table:nth-child(1)')
webElem$getElementText()
我正在尝试从 table 中抓取一些使用下拉菜单的数据。 table 是“GIC 选项”。 “Term”为“Mid-Term”,“Interest Type”为“Compound”。
查看 SO 答案
library(RSelenium)
remDr <- remoteDriver(port = 4445L)
remDr$navigate("https://www.nbc.ca/personal/savings-investments/gic/non-redeemable.html")
webElem <- remDr$findElement(using = "xpath", '//*[@class="ft2-dropdown-list-element"]/option[@value="Mid-Term"]')
和一个css版本
webElem <- remDr$findElement(using = "css", "#ft2-filterDropdownBtnID option[value='Mid-Term']")
两者都导致 Selenium message:Unable to locate element
如何抓取此 table 中的数据?
下面是我的解决方案。
library(RSelenium)
driver <- rsDriver(browser=c("firefox"),port = 4445L)
remote_driver <- driver[["client"]]
remote_driver$navigate("https://www.nbc.ca/personal/savings-investments/gic/non-redeemable.html")
#Active the box by a click
remote_driver$findElement(using = "css selector", '.ft2-dropdown-btn-label')$clickElement()
#Now you can choose what you need
remote_driver$findElement(using = "xpath", '//*[@id="ft2"]/div[2]/div/ul/li[3]/a')$clickElement()
#All
//*[@id="ft2"]/div[2]/div/ul/li[1]/a
#Short term
//*[@id="ft2"]/div[2]/div/ul/li[2]/a
#Mid term
//*[@id="ft2"]/div[2]/div/ul/li[3]/a
#Long term
//*[@id="ft2"]/div[2]/div/ul/li[4]/a
#To scrape the value of the table, below a possible solution.
webElem <- remote_driver$findElement(using = "css selector", 'div.table-wrapper:nth-child(4) > table:nth-child(1)')
webElem$getElementText()