问题
I'm using WatiN and C# to fill out a form online but my Search field has the same name as another element. My search field is defined as follows:
<input type="text" value="" size="50" name="search">
There is also a different search field in another section of the same screen defined as:
<input class="Search-TextBox" type="text" value="" size="15" name="search">
I want to fill out the formerly mentioned text input search field. Is there a way to find and fill the search box based on size maybe?
I have the same issue with the search field's Submit button. Its the same name and size so how do I specify which button to click search on?
回答1:
You can use any of the following mechanisms
browserinstance.TextField(t => t.Name == "search" && t.GetAttributeValue("size") == "50").Value = "value";
//OR
browserinstance.TextField(Find.ByName("search").And(Find.By("size","50"))).Value = "value";
If all the attributes are the same, you can search on the basis of the ordering after getting a filtered list.
browserinstance.TextFields.Filter(Find.ByName("search"))[0].Value = "value";
来源:https://stackoverflow.com/questions/8175699/how-to-fill-a-specific-input-field-with-watin-when-multiple-inputs-have-the-sam