lefthandedgoat / canopy

f# web automation and testing library, built on top of Selenium (friendly to c# also)
http://lefthandedgoat.github.io/canopy/
MIT License
507 stars 115 forks source link

Cannot find element with id which contains special characters like '|', '.' #394

Closed yan0lovesha closed 6 years ago

yan0lovesha commented 6 years ago

If an element id contains some special characters like '|' or '.', the element and elements function cannot find it.

Example:

Given an element id is "systemuser|systemuser.PromoteToAdmin". Then element "#systemuser|systemuser.PromoteToAdmin" cannot find it.

If I remove the '|' and '.' from the id, then it can be found by element function.

It is working to use C# api like: browser.FindElement(By.Id("systemuser|systemuser.PromoteToAdmin"));;

I didn't test other characters. There may be other letters also has this problem.

lefthandedgoat commented 6 years ago

I think it would work with '|' but it won't work with '.' because a period is class in a CSS selector.

canopy attempts several selector types:

yield findByCss                cssSelector f
yield findByValue              cssSelector f
yield findByXpath              cssSelector f
yield findByLabel              cssSelector f
yield findByText               cssSelector f
yield findByJQuery             cssSelector f
yield findByNormalizeSpaceText cssSelector f

Your selector would be treated like this when run as a css selector

find element with id of systemuser|systemuser with immediate child of class PromoteToAdmin

You can add the By.Id to the list of things that canopy will automatically look for like this (put this in your setup code, around where you start a browser or change configurations)

let findById id f =
    try
        f(By.Id(id)) |> List.ofSeq
    with | ex -> []

addFinder findById

Hope this helps!