How to loop through FORM elements?

Posted on 2005-05-06
Last Modified: 2013-11-19
The following code will allow me to loop through FORM tags but not the elements in them.  I may have five forms on one page.  How do I loop through form elements in the forth FORM?

private SHDocVw.InternetExplorer IE_Inst = new SHDocVw.InternetExplorerClass();
mshtml.IHTMLDocument2 HTMLDocument =
      (mshtml.IHTMLDocument2) this.IE_Inst.Document;
                        mshtml.IHTMLElementCollection forms = HTMLDocument.forms;

      foreach (mshtml.HTMLFormElementClass el in forms)
      strType = el.outerHTML;

Once I'm in a particular FORM, I want to find certain input fields and fill in values.

Question by:brettr

    Author Comment

    I'm currently getting around the problem by looping through all elements in the Doc:

        foreach (mshtml.IHTMLElement wbrElm in wbrAll)
         // Assign the inner html values of the input to our variables
         //look for only INPUT types
         if(wbrElm.tagName.ToLower() == "input" && wbrElm.outerHTML.IndexOf("name", 1) > 0)
          strName = wbrElm.getAttribute("name", 0).ToString();
          Debug.WriteLine(wbrElm.tagName.ToLower() + " -- " + wbrElm.outerHTML);
          // We are only interested in filling text boxes,
          // and only interested in a specific  one, i.e. "q"    

          if (strName != null && strName.ToLower() == userNameFormField)          // Set the "value" with the setAttribute method.
           wbrElm.setAttribute("value", userNameValue, 0);

          if (strName != null && strName.ToLower() == passwordFormField)          // Set the "value" with the setAttribute method.
           wbrElm.setAttribute("value", passwordValue, 0);
        } //end for each

    Not the best technique.

    LVL 96

    Accepted Solution

    Actually, I believe that it is the only way, because it is not like XML/XPath where you can select nodes.


    Author Comment

    Well, while we're at, I can do this in VB:

    IE_Inst = SHDocVw.InternetExplorer

    How can I do it in C#?  There isn't a Forms part.
    LVL 96

    Expert Comment

    by:Bob Learned
    That, my friend, is a great question, and one in which I don't have an answer.  There appears to be a difference in the way that C# imports the COM library, versus VB.  I didn't know that there was the Forms object, and again I have learned something new.


    Author Comment

    Well, I've abandoned looping through forms and have decided to post to a specific form in the page by doing:

    object noValue = System.Reflection.Missing.Value;
    string oURL = "";
    string cSeparator = "&";
    string cPostData = "field1=value1" + cSeparator
    + "field2=value2";
    object vHeaders = "Content-Type: application/x-www-form-urlencoded" + "\n" + "\r";
    object vPost = ASCIIEncoding.ASCII.GetBytes(cPostData);
    this.IE_Inst.Navigate(oURL, ref noValue, ref noValue, ref vPost, ref vHeaders);

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
    JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    The viewer will learn how to count occurrences of each item in an array.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now