【问题标题】:RSelenium fails to find element on page with dropdownsRSelenium 无法在带有下拉菜单的页面上找到元素
【发布时间】:2020-11-14 03:27:27
【问题描述】:

我正在尝试从使用下拉列表的表中抓取一些数据。该表是“GIC 选项”。 “期限”是“中期”,“兴趣类型”是“复合”。

看着SO答案here,我试过了:

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

如何抓取此表中的数据?

【问题讨论】:

    标签: r rselenium


    【解决方案1】:

    在我的解决方案之下。

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

    【讨论】:

    • 你是如何得到这些 xpaths 的,//*[@id="ft2"]/div[2]/div/ul/li[1]/a 等等...
    • 我是点击列表后推导出来的。
    猜你喜欢
    • 2015-10-15
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 2020-10-01
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    相关资源
    最近更新 更多