views:

63

answers:

2

I'm automating form login in a certain site using the WebBrowser control. Having the client id of the textbox, password and button login, I'm able to do it.

The question is, can I still do it without identifying the id of the button and simulate a click?

A: 

Yes; you can call the submit method of the form object.

For example:

browser.Document.Forms[0].Invokemember("submit");

To set the name and password, loop through

browser.Document.Forms[0].GetElementsByTagName("input")

Tpassword will have the type attribute equal to password, and the username will probably be just before the password.

SLaks
@S.Laks: he also needs to fill in username and password.
John Saunders
Yes, but he didn't ask to.
SLaks
@SLaks: he kind of did: "With the client id of the textbox, password and button login, i was able to do it." I can't think why else he'd have asked about those controls.
John Saunders
+1  A: 

There is no way you could do this without being able to identify the controls somehow. You would need the ids, or you would need to be told exactly where in the DOM they were located by some other means.

John Saunders
Wrong. See my edit.
SLaks
@Slaks: right, in general. What if `type="password"` isn't used? What if there's a lot of `<input type="hidden"/>` between the password and username boxes?
John Saunders
What? Show me _one_ login form, anywhere, without an `<input type="password">`. You can ignore hidden inputs.
SLaks
@SLaks: there also may be more than one "password" box. What about for answers to secret questions? What if they have the "Change Password" on the same form, with two "password" boxes, both display:none?
John Saunders
john saunders right. actually, what im after is automating it without defining the login button id. username and password field is alright to define
Jepe d Hepe