【问题标题】:F# Canopy: Not picking up certain page elementsF# Canopy:不拾取某些页面元素
【发布时间】:2024-05-18 07:20:02
【问题描述】:

我是 F# Canopy 的新手,我正在测试在 https://www.treasurydirect.gov/GA-FI/FedInvest/selectSecurityPriceDate.htm 的输入框中输入日期。运行下面的 Canopy 代码时,我收到一个“用户未处理”异常,说明“canopy.types.ConaopyElementNotFoundException: 'can't find element id=priceDate.month'”虽然可以在页面源中看到该元素以及用于 Chrome 的 Selenium Page Object Generator 和 Selenium Object Finder 扩展。似乎对于某些页面对象,Canopy 无法识别这些元素……或者我遗漏了一些东西。有什么想法吗?

open System
open canopy
open canopy.runner.classic
open canopy.configuration
open canopy.classic

[<EntryPoint>]
let main argv =
    canopy.configuration.chromeDir <- System.AppContext.BaseDirectory

    //start an instance of chrome
    start chrome

    "testing UST prices" &&& fun _ ->
        //this is an F# function body, it's whitespace enforced

        //go to url
        url "https://www.treasurydirect.gov/GA-FI/FedInvest/selectSecurityPriceDate.htm"

        click "id=priceDate.month"
        "id=priceDate.month" << "3"
        click "id=priceDate.day"
        "id=priceDate.day" << "31"
        click "id=priceDate.year"
        "id=PriceDate.year" << "2020"

        click "Show Prices"
        click "CSV Format"


    //run all tests
    run()

    printfn "press [enter] to exit"
    System.Console.ReadLine() |> ignore

    quit()

    0

【问题讨论】:

    标签: f# canopy canopy-web-testing


    【解决方案1】:

    看起来你的 XPath 写错了。

    这会起作用

     click "//*[@id='priceDate.month']"
            "//*[@id='priceDate.month']" << "3"
     click "//*[@id='priceDate.day']"
            "//*[@id='priceDate.day']" << "31"
     click "//*[@id='priceDate.year']"
            "//*[@id='priceDate.year']" << "2020"
    

    找到正确 XPath 的解决方案是使用 Chrome 中的开发人员工具。 右键单击元素并复制 -> 复制 XPath

    【讨论】:

      【解决方案2】:

      Selenium(以及随之而来的树冠)支持 CSS

      click "#elemId"
      

      XPath

      click "//*[@id='elemId']"
      

      选择器。

      【讨论】: