How to get a list of elements with Watin (c#) and change values?

272 Views Asked by At

i want use watin to get list of elements in a webpage and change all the values

for example i want find all textboxes with "test" ID and change their values

this is my code. but it only change one element, not all ( in my code i looked for elements via thir name )

  IE ie = new IE("http://sample.net/");
  ie.TableRow(Find.ByText(t => t.Contains("sample text"))).TextField(Find.ByName("second text")).TypeText("write this string");
  ie.WaitForComplete();

in this code i find a text in a table, then i find text box (second text) , then it write "write this string" but it i want to do this for all the similar elements i tried with foreach but failed anyone knows the right code?

1

There are 1 best solutions below

0
Sham On

I can see that you have targeted to a TableRow and used TextField which would be only a textfiled. You should be targeting parent and try to get all the text fields with your criteria.

I have tried with below code and worked for me. If you still unable to figure out the issue, you post the error/html source code.

 IE ie = new IE("https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ltmpl=default");
 ie.WaitForComplete();

 foreach (var txtEle in ie.Form(Find.ById("createaccount")).TextFields)
 {
      if (!string.IsNullOrEmpty(txtEle.Id))
      {
          if (txtEle.Id.Contains("Name"))
          {
             txtEle.TypeText("write this string");
          }
      }
  }

 ie.WaitForComplete();