5

Hello I am trying to automate my IE to log into a website but the problem is that the input elements do not have an HTML ID attribute! For example:

<input type="text" name="user" size="15" value="">

How do you program C# to insert text in this text box?

Thanks

5 Answers 5

6

Add the following attributes to your input tag: runat="server" and id="someId"

<input id="user" type="text" size="15" runat="server">

Then server-side you do:

user.Text = "sample text";

Then you can do something like:

foreach (Control c in Page.Controls)
{
    TextBox t = c as TextBox;

    if (t != null)
    {
        t.Text = "sample text";
    }
}

But I'm not sure it'll work without the runat="server" attribute

Sign up to request clarification or add additional context in comments.

2 Comments

thanks but I don't have access to edit the HTML on the website :(
I think he's just trying to post to a different website that isn't under his control
4

I know this is a bit late, but here is an alternate to the jquery method.

I assume with IE you mean the webbrowser control. Once you have the document, you can go through the input -elements.

Something like

HtmlElementCollection inputs = browser.Document.GetElementsByTagName("input");

Then you loop through each of the inputs. You can check the input's name with input.GetAttribute("name").Equals("user")

Inserting value to the field would be done with

input.SetAttribute("value", "MyUserName");

Comments

2

I guess this isn't "doing it programatically with C#", but you could jQuerify the page, then run some custom javascript afterwards, to manipulate the value of the control. If you are using WebBrowser, you could call the below method to insert the scripts.

string script = "script text";
WebBrowser.Navigate(script);

jQuerify code

var s=document.createElement('script');
s.setAttribute('src','http://jquery.com/src/jquery-latest.js');
document.getElementsByTagName('body')[0].appendChild(s);

Custom code

$(document).ready(function(){$('input[type="text"][name="user"]').val('FooBar')});

Comments

2

Maybe this can help:-

  • NB : also look at http://msdn.microsoft.com/en-us/library/2te2y1x6.aspx http://msdn.microsoft.com/en-us/library/system.web.ui.htmltextwriter.aspx http://social.msdn.microsoft.com/Search/en-US/?query=mshtml%20tutorial&ac=1

  • Create a new project like a Windows form application project,

  • Add references of MSHTML ie Microsoft HTML Object Library plus SHDocVw ie Microsoft Internet Controls,

  • Create a function with body somewhat like and bound it to anything like a button's click event:

            /*INTERNET EXPLORER's OBJECT*/
            SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
    ie.Navigate("http://www.example.com/entry"); /*GO TO EXAMPLE.COM*/
            /*WAIT UNTIL THE BROWSER IS READY AND COMPLETELY LOADED*/
    while (ie.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE) 
            {
               Application.DoEvents();
            }
            mshtml.HTMLDocument doc = ie.Document;
            while (doc.readyState != "complete")
            {
               Application.DoEvents();
            }
    /*GET ALL THE INPUT ELEMETS IN A COLLECTION*/
    MSHTML.IHTMLElementCollection collection=
            doc.getElementsByTagName("INPUT");
            foreach (mshtml.IHTMLElement elem in collection)
            {
              if (elem.getAttribute("name") != null)
                {
                  /*IDENTIFY THE INPUT CONTROL BY NAME ATTRIBUTE*/
          if (elem.getAttribute("name").Equals("user"))
                  {/*ENTER USER NAME*/
                   elem.setAttribute("value", "ABC");
          }
        }
    }                           
    

Comments

1

As Nico said with:

<input id="user" type="text" size="15" runat="server">

but you must try:

user.Value = "sample text";

insted!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.