Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do You Set Value of Input Element Programmatically Through CSharp?

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

like image 668
Nabisco_DING Avatar asked Oct 21 '10 18:10

Nabisco_DING


4 Answers

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

like image 128
bevacqua Avatar answered Oct 21 '22 16:10

bevacqua


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");

like image 36
Cubicle Avatar answered Oct 21 '22 16:10

Cubicle


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')});
like image 2
sshow Avatar answered Oct 21 '22 15:10

sshow


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");
          }
        }
    }                           
    
like image 2
xameeramir Avatar answered Oct 21 '22 16:10

xameeramir