Skip to content Skip to sidebar Skip to footer

Watin Can't Select An Option From A Select List

I am using WATIN to complete a dynamically created form, which can contain several SelectLists (these are javascript controlled picklists). A sample of the select list format outpu

Solution 1:

The following is somewhat not pretty.... but... it works.

Using

I was unable to reproduce your index error; I can access the SelectList options by index without issue (see code below). BUT... accessing them didn't help at as Chosen has its own presentation markup.

So, instead I worked with the Chosen HTML elements instead of the SelectList and things are working better.

[Test]
public void ChosenTest()
{
    IE myIE = new IE(true);
    myIE.GoTo("http://davidwalsh.name/dw-content/jquery-chosen.php");

    myIE.SelectList(Find.ByClass("chosen chzn-done")).WaitUntilExists();  //Needed as sometimes the controls were not fully loaded; unable to get item not found exceptions once this was added.

    Console.WriteLine("ByIndex:" + myIE.SelectList(Find.ByClass("chosen chzn-done")).Options[3].Text);   //To show no index out of bounds error.
    //  Just for reference --> myIE.SelectList(Find.ByClass("chosen chzn-done")).Options[3].Select();  //Has no effect.

    string id = myIE.SelectList(Find.ByClass("chosen chzn-done")).Id;
    myIE.Div(id + "_chzn").Div(Find.ByClass("chzn-drop")).ElementWithTag("li", Find.ByIndex(3)).Click();
    myIE.Div(id + "_chzn").Links[0].Spans[0].Click();  //Needed or the SelectList-ish stays open.
}

Finding ByClass was done as the control IDs change on the example page. The WaitUntilExists eliminated the intermittent failures.


Post a Comment for "Watin Can't Select An Option From A Select List"