I want to use a webBrowser control in visual studio to automate page navigation. Anyone have any resources that may be of assistance? I have done quite a bit of searching but can't find anything useful.
If there is perhaps a better forum to post this particular question could the moderator please move the post, thanks.
automate webBrowser navigation
Started by hoser2001, Sep 02 2007 07:54 PM
9 replies to this topic
#1
Posted 02 September 2007 - 07:54 PM
|
|
|
#2
Guest_Jordan_*
Posted 03 September 2007 - 06:44 AM
Guest_Jordan_*
I'm not sure what you are asking, what do you mean by automate?
#3
Posted 03 September 2007 - 07:42 AM
There is a web page that has two text boxes (username, password) and a submit button. I want to automatically fill the text boxes and click the submit button.
here is the source for the page
I have figured out how to fill the text boxes in, but for some reason I can't figure out how to click the submit button... here's my code:
I'm using the microsoft web browser component.
The web page is www(dot)beapirate(dot)net
The code I have works in terms of filling in the text boxes, but I could not figure out how to find the button... So I decided to try and call the php script to log in, however it appears to not be able to see that I have supplied username and password, any ideas?
here is the source for the page
<HTML> <head><title>The Pirate Game</title> <link rel="shortcut icon" href="/pirates/favicon.ico" > <link href="pirates.css" rel="stylesheet" type="text/css"> </head><h2>The Pirate Game -- Login</h2><img src=images/welcome.png class=welcome><div class=welcome> <form name=login method=post action=attemptlogin.php><table><TR> <TD>Username</td><TD><input type=text name=username></TD></tr><tr> <TD>Password</td><TD><input type=password name=password></TD></tr><tr> <TD></td><TD align=right><input value="Sign In" class="transOFF" onmouseover="this.className='transON'" onmouseout="this.className='transOFF'" type=submit><BR><a href=register.php>Sign Up</a></TD></tr></table> </form></div></body> </html>
I have figured out how to fill the text boxes in, but for some reason I can't figure out how to click the submit button... here's my code:
private void axWebBrowser1_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
try
{
HTMLDocument myDoc = new HTMLDocumentClass();
myDoc = (HTMLDocument)axWebBrowser1.Document;
HTMLInputElement user_txtbox = (HTMLInputElement)myDoc.all.item("username", 0);
user_txtbox.value = "username";
HTMLInputElement pass_txtbox = (HTMLInputElement)myDoc.all.item("password", 0);
pass_txtbox.value = "password";
object loc = "http://www.beapirate.net/pirates/attemptlogin.php";
this.axWebBrowser1.Navigate2(ref loc, ref null_obj, ref null_obj, ref null_obj_str, ref null_obj_str);
//HTMLInputElement btnLogin =(HTMLInputElement)myDoc.all.item("logon", 0);
//btnLogin.click();
}
catch (Exception ex) { MessageBox.Show(ex.Message + " " + ex.StackTrace); }
}
I'm using the microsoft web browser component.
The web page is www(dot)beapirate(dot)net
The code I have works in terms of filling in the text boxes, but I could not figure out how to find the button... So I decided to try and call the php script to log in, however it appears to not be able to see that I have supplied username and password, any ideas?
#4
Posted 03 September 2007 - 03:36 PM
I'm going out on a limb here but couldn't you simply just focus on btnLogin and then simulate pressing enter?
You may not even have to focus on the submit button, just the form itself.
SendKeys.SendWait(Chr(13))
You may not even have to focus on the submit button, just the form itself.
#5
Posted 03 September 2007 - 03:44 PM
btnLogin is the object, I need to somehow bind it to the actual button on the page...which I can't do because i dont' know the name. there is no "logon" or any other name attribute...
#6
Guest_Jordan_*
Posted 04 September 2007 - 07:59 AM
Guest_Jordan_*
Going from what Dirkfirst said, couldn't you just focus on the login and press enter?
#7
Posted 04 September 2007 - 03:58 PM
that what i tried to do with this code:
but that doesnt work.
//HTMLInputElement btnLogin =(HTMLInputElement)myDoc.all.item("logon", 0);
//btnLogin.click();
but that doesnt work.
#8
Posted 05 September 2007 - 04:58 PM
I have your solution.
I'm not sure if your program uses Windows.Forms, so this might not help at all. But, if you're using System.Windows.Forms.WebBrowser to load your html form:
HtmlElement el = webBrowser1.Document.All["mybutton"];
object obj = el.DomElement;
System.Reflection.MethodInfo mi = obj.GetType().GetMethod("click");
mi.Invoke(obj, new object[0]);
or, if you're using mshtml:
HtmlElement el = webBrowser1.Document.All["mybutton"];
mshtml.HTMLInputElement el2 = (mshtml.HTMLInputElement)el.DomElement;
el2.click();
I'm not sure if your program uses Windows.Forms, so this might not help at all. But, if you're using System.Windows.Forms.WebBrowser to load your html form:
HtmlElement el = webBrowser1.Document.All["mybutton"];
object obj = el.DomElement;
System.Reflection.MethodInfo mi = obj.GetType().GetMethod("click");
mi.Invoke(obj, new object[0]);
or, if you're using mshtml:
HtmlElement el = webBrowser1.Document.All["mybutton"];
mshtml.HTMLInputElement el2 = (mshtml.HTMLInputElement)el.DomElement;
el2.click();
#9
Posted 06 September 2007 - 03:38 AM
the example you posted for mshtml is pretty much the exact same thing I just said doesn't work. The problem with that code is I dont know what "mybutton" is. The button doesn't have a name on the form.
#10
Posted 06 September 2007 - 04:41 AM
ok, not a problem. Here is your code.
HtmlDocument doc = webBrowser1.Document;
HtmlElement elm = doc.All["username"];
elm.SetAttribute("value", "YourUserName");
HtmlElement elmPW = doc.All["password"];
elmPW.SetAttribute("value", "YourPassword");
HtmlElement frm = doc.Forms["login"];
frm.InvokeMember("submit");
HtmlDocument doc = webBrowser1.Document;
HtmlElement elm = doc.All["username"];
elm.SetAttribute("value", "YourUserName");
HtmlElement elmPW = doc.All["password"];
elmPW.SetAttribute("value", "YourPassword");
HtmlElement frm = doc.Forms["login"];
frm.InvokeMember("submit");


Sign In
Create Account


Back to top









