Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to loop through FORM elements?

Posted on 2005-05-06
5
Medium Priority
?
1,611 Views
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.

Thanks,
Brett
0
Comment
Question by:brettr
  • 3
  • 2
5 Comments
 

Author Comment

by:brettr
ID: 13946749
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.

Brett
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 375 total points
ID: 13947564
Actually, I believe that it is the only way, because it is not like XML/XPath where you can select nodes.

Bob
0
 

Author Comment

by:brettr
ID: 13947830
Well, while we're at, I can do this in VB:
this.IE_Inst.Document.Forms(formIndex).Submit();

where
IE_Inst = SHDocVw.InternetExplorer

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

Expert Comment

by:Bob Learned
ID: 13959279
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.

Bob
0
 

Author Comment

by:brettr
ID: 13959679
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 = "http://www.somewhere.com";
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);
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

571 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