开发者

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

开发者 https://www.devze.com 2023-01-21 08:01 出处:网络
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:

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 p开发者_运维问答rogram C# to insert text in this text box?

Thanks


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


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


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')});


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


As Nico said with:

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

but you must try:

user.Value = "sample text";

insted!

0

精彩评论

暂无评论...
验证码 换一张
取 消