RSelenium이 드롭 다운이있는 페이지에서 요소를 찾지 못함

Nov 13 2020

드롭 다운을 사용하는 테이블에서 일부 데이터를 긁어 내려고합니다. 표는 "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

이 테이블의 데이터를 어떻게 스크랩 할 수 있습니까?

답변

1 EarlMascetti Nov 14 2020 at 07:15

내 솔루션 아래.

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