RSelenium findet kein Element auf einer Seite mit Dropdowns
Nov 13 2020
Ich versuche, einige Daten aus einer Tabelle zu entfernen, die Dropdowns verwendet. Die Tabelle lautet "GIC-Optionen". Der "Term" ist "Mid-Term" und der "Interest Type" ist "Compound".
Als ich mir die SO-Antwort hier ansah , versuchte ich:
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"]')
und eine CSS-Version
webElem <- remDr$findElement(using = "css", "#ft2-filterDropdownBtnID option[value='Mid-Term']")
Beides führt zu Selenium message:Unable to locate element
Wie kann ich die Daten in dieser Tabelle kratzen?
Antworten
1 EarlMascetti Nov 14 2020 at 07:15
Unten meine Lösung.
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()