Jump to content

automate webBrowser navigation

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
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.

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I'm not sure what you are asking, what do you mean by automate?

#3
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
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



	<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
dirkfirst

dirkfirst

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 354 posts
I'm going out on a limb here but couldn't you simply just focus on btnLogin and then simulate pressing enter?

SendKeys.SendWait(Chr(13)) 

You may not even have to focus on the submit button, just the form itself.

#5
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
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_*

Guest_Jordan_*
  • Guests
Going from what Dirkfirst said, couldn't you just focus on the login and press enter?

#7
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
that what i tried to do with this code:

//HTMLInputElement btnLogin =(HTMLInputElement)myDoc.all.item("logon", 0);

                //btnLogin.click();



but that doesnt work.

#8
mluker

mluker

    Newbie

  • Members
  • Pip
  • 7 posts
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();

#9
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
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
mluker

mluker

    Newbie

  • Members
  • Pip
  • 7 posts
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");