Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue With Select List Manipulation #176

Closed
bp176 opened this issue Dec 3, 2014 · 3 comments
Closed

Issue With Select List Manipulation #176

bp176 opened this issue Dec 3, 2014 · 3 comments

Comments

@bp176
Copy link

bp176 commented Dec 3, 2014

Posted on stackoverflow: http://stackoverflow.com/questions/27250994/canopy-working-with-select-lists

I have just started working with Canopy and am struggling with select lists. I know you can set the selected item by value by doing:

"#dropDown" << read "option[value='x']"

However, how would you select the nth element of a select list? Or, how about selecting by text? I can't seem to find anything in the documentation.

I tried writing some helpers to select the first non-blank option in a select list. The weird thing is that it works the first time I call it, but every subsequent call to the helper method times out. I'm baffled and also an F# newbie ;)

let selectFirstOption selector = 
    let s = elements (sprintf "%s option" selector)
               |> Seq.toArray
               |> Array.tryFind (fun el -> el.Text <> "")

    let v = match s with
              | Some s -> s.GetAttribute "Value"
              | None -> failwith "No option found"

    selector << read (sprintf "option[value='%s']" v)
@lefthandedgoat
Copy link
Owner

You can do this with xpath

go to this page http://lefthandedgoat.github.io/canopy/testpages/

using chrome dev tools run this command from console (chrome dev tools will run xpath selectors using $x() )

$x("id('states')/option[@value != ''][1]")

This will find the element with id of state, find all options with value != '' and return the 1st one.

Your helper would look something like this (not tested)

let selectFirstOption selector =     
    let firstOptionWithValue = element selector  |> elementWithin "/option[@value != ''][1]"
    selector << read firstOptionWithValue 

This will fail if there is no option inside the selector your provided, so you can also use someElementWith and match for Some/None like in your example
Documentation on that is here: http://lefthandedgoat.github.io/canopy/actions.html

Technically when you use << on a select it just finds the option with the text you provide (from read) and clicks it, you can see that here:

https://github.com/lefthandedgoat/canopy/blob/master/src/canopy/canopy.fs#L297

So you may just want to do this (also not tested):

let selectFirstOption selector =    element selector  |> elementWithin "/option[@value != ''][1]" |> click

@bp176
Copy link
Author

bp176 commented Dec 3, 2014

That gives me something to work with. Thanks a lot!

@lefthandedgoat
Copy link
Owner

For nth element you can do:

"#states" << read <| nth 3 "#states option"

or

"#states" << read (nth 3 "#states option")

For by text (or value) this works

"#states" << "1" //for alabama as its value is 1
"#states" << "Alabama" //also works

lefthandedgoat added a commit that referenced this issue Jan 22, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants