Login in a Webpage with Watin

2019-07-19 19:56发布

问题:

I try to login on a webpage. On the webpage are two forms with inputs, the inputs have the same Id("username").

How can i get the right inputs to set my text?

This is my wrong Code:

browser.TextField(Find.ByName("username")).TypeText("test123");

or

browser.Form(Find.ByName("form_login")).TextField(Find.ByName("username")).TypeText("test123");

回答1:

You can collect all text field in the page and then perform the action on the first or second appearance of the desired id.

For example:

        TextFieldCollection textFields = browser.TextFields;
        foreach (var field in textFields)
        {
            if (field.Id == "username")
            { 
                //do something
            }
        }