簡體   English   中英

RSelenium 無法在帶有下拉菜單的頁面上找到元素

[英]RSelenium fails to find element on page with dropdowns

我正在嘗試從使用下拉列表的表中抓取一些數據。 該表是“GIC 選項”。 “期限”是“中期”,“利息類型”是“復合”。

這里查看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

如何抓取此表中的數據?

在我的解決方案下面。

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

暫無
暫無

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

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