Keith 8o8
Keith 8o8

Reputation: 21

WatiN SelectList - How can I select any element from the list?

I'm using WatiN to do some web testing and have run into a problem with a select list. I have to run through a few pages first, adding a 'Category' element that will populate said troublesome select list. I have been able to easily select an element from the list using ByValue, but the problem here is that the values is more of an index element that is created on the fly with a seemingly random value when the 'Category' element is created. I have tried to use the text that is under the option list in the html but it cannot seem to find it. At this point I'm willing to settle for any element in the list that isn't value "-1" as this is the "Please select an option item".

Any help at all would be appreciated, Thanks in advance

Keith 8o8

Upvotes: 2

Views: 3846

Answers (2)

Stathis Andronikos
Stathis Andronikos

Reputation: 1259

Here is a solution :

 SelectList list = _browser.Frame(Find.ById(frameId)).SelectList(listId);
            foreach (Option tempOption in list.Options)
            {
                string value = tempOption.Text;
                if (!string.IsNullOrEmpty(value)) // Compare with visible option text
                {
                    if (value.Equals(option))
                    {
                        list.Option(option).Select();
                        found = true;

Upvotes: 0

alonp
alonp

Reputation: 1337

If i understand correctly maybe this can help:

 OptionCollection options = browser.SelectList("elementId").Options;
 options[0].Select();

Upvotes: 3

Related Questions