1

我是 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
4

2 回答 2

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

于 2020-05-14T17:28:49.710 回答
0

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

click "#elemId"

XPath

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

选择器。

于 2020-05-14T21:04:16.357 回答