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
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')});
As Nico said with:
<input id="user" type="text" size="15" runat="server">
but you must try:
user.Value = "sample text";
insted!