views:

169

answers:

4

Using C# WATIN, how do I get the value of the second occurence of a INPUT tag html element on a page?

I've tried a whole bunch of things and haven't succeeded. The debugger isn't giving me any obvious way to get element[1] among the 4 that returned.

Console.WriteLine(ie.Element(Find.ByClass("myclass")).GetAttributeValue("value")  ) ;  
// works but prints the value of the 1st of 4 input elements on the page

Console.WriteLine(ie.ElementsWithTag("input", "text").Length.ToString()  ); 
// returns the number 4

Console.WriteLine( ie.Element(Find.ByName("login")).GetAttributeValue("value")  );
// works but its not safe since its possible there might be two elements with the name login

Basically, I want to be able to select the input element by its array index.

A: 

Try

ie.Element(Find.ByIndex(1))
prostynick
+1  A: 

This can help:

        ElementCollection elementCol = ie.Elements;
        elementCol = elementCol.Filter(Find.ByClass("myclass"));
        string value = elemntCol[1].GetAttributeValue("value");
alonp
great idea! thanks. ;-)
djangofan
A: 

Doesn't anybody use loops anymore? I think loops are neat (and a whole lot easier on the comprehension of why Watin exhibits this behavior / stole my lunch ;-) :

TextFieldCollection textFields = browser.TextFields;  
foreach (var field in textFields)  
{  
    if (field.Id == "humbuggery")...  
} 
Morten Bergfall
...more loop-like, I meant :-)
Morten Bergfall
A: 

I believe that I may have accidentally ran across a better answer from index constraint class, which is:

ie.Link(new IndexConstraint(1) && Find.ByName("linkname"))

I need to research it. I'll revisit this question later.

djangofan
@sth - thanks for the edit. that was nice of you.
djangofan