Share via


WebBrowser Input data and get data

Question

Sunday, February 8, 2009 1:40 AM

I have been trying to get a complete example on the following
Open a website using the WebBrowser control in VB.net 2008
Enter the UserID and password
Click on the Login button
Then, on the next page, enter all the form fields and submit
On a different page, read the results

Can you please help me with the source code ?

Thank you 

All replies (5)

Friday, February 13, 2009 7:28 AM ✅Answered

Hi Subumysore,

You problem is about how to automate web page via WebBrowser.

1) Basic principle:

Firstly load web page on WebBrowser object, then use GetElementsByTagName or GetElementsByID function to locate webpage elements in WebBrowser.Document and automate them (

e.g. retrieve page text, login website, search, click button or hyperlink).

 

Beforehand, we need to find out web elements’ source html code via viewing source (Right-click on web page -> "View Source" menu item).

 

For example: How to automatically login in one website (input uername/password and click Login button)?

Assume that this page http://www.website.com/login.aspx has the following elements:

<input name="UserNameTextBox" type="text" value="myUser" id="UserNameTextBox">

<input name="PasswordTextBox" type="text" value="myUser" id="PasswordTextBox">

<INPUT type=submit value="Login" name="LoginButton">

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Part 1: Use WebBrowser control to load web page

        WebBrowser1.Navigate("http://www.website.com/login.aspx")

 

        System.Threading.Thread.Sleep(2000) ' Delay 2 seconds to render login page

 

        ' Part 2: Automatically input username and password

        Dim theElementCollection As HtmlElementCollection

        theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")

        For Each curElement As HtmlElement In theElementCollection

            Dim controlName As String = curElement.GetAttribute("name").ToString

            If controlName = "UserNameTextBox" Then

                curElement.SetAttribute("Value", "Username text here")

            ElseIf controlName = "PasswordTextBox" Then

                curElement.SetAttribute("Value", "Password text here")

'In addition,you can get element value like this:

                'MessageBox.Show(curElement.GetAttribute("Value"))             End If         Next

 

        ' Part 3: Automatically clck that Login button

        theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")

        For Each curElement As HtmlElement In theElementCollection

            If curElement.GetAttribute("value").Equals("Login") Then

                curElement.InvokeMember("click")

                ' javascript has a click method for we need to invoke on button and hyperlink elements.             End If         Next     End Sub End Class

 

2) Some code samples about WebBrowser Automation:

Automatically login in Yahoo website (Input uername/password and click Login button)

http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/0b77ca8c-48ce-4fa8-9367-c7491aa359b0/

 

Automatically Windows Live search or Google (Enter search keyword and click Search button)

http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/f06fcdca-ed9b-464b-ba08-edd26b0dc801/

 

Locate html elements: DropDown list / Radio Button and RichTextBox

http://forums.msdn.microsoft.com/en-US/vbgeneral/thread/4ec55500-56a7-4656-b4dd-1e5871fc9806/

 

Locate Hyperlink element and retrieve URL value from "href" attribute, then use My.Computer.Network.DownloadFile method to download file.

http://forums.msdn.microsoft.com/en-US/vbgeneral/thread/6cd60d72-0695-4408-bf40-d858aefc8945/

 

3) Note: As for table data (without enough attributes to locate them) on web page, we need to try Regular Expression (System.Text.RegularExpressions namespace).

Related thread:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2485067&SiteID=1

Regular Expressions Forum is here:

http://social.msdn.microsoft.com/forums/en-US/regexp/threads/

Best Regards

Yichun Feng

Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Sunday, February 8, 2009 2:29 AM

What happens if they are not logged on?

Renee


Tuesday, February 10, 2009 5:25 AM

Hi Subumysore,

Where do your UserId and password come from?

If it is from your own database,  I think there is not a direct way to achieve what you want. Because you should build up a new ASP.NET website first, then you use your webbrowser to visit your site.

Here is an example how use webbrowser  navigating to the web site:

WebBrowser1.Navigate( _  
        New Uri("http://msdn.microsoft.com/zh-cn/library/system.windows.controls.webbrowser.aspx")) 

And you can just use textbox and button to check whether this user is valid. How to check:(Arjun's answer)

http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/cd5aa46d-23ef-4cff-97d2-490ff40158f5/

If you want to visit website which has already exist, can you give more details about your problem? 

 

 

Best Regards

Yichun Feng

 

 

  


Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Tuesday, February 10, 2009 7:25 AM

 What I am trying to do is this.....

There is a website...NOT OWNED BY US....say https://www.questions.com
I need to programmatically go to this site ( as you have shown above).
Now, I do have my USERID and PW...which I need to input into the URL's respective fields
 and carry on the rest of my requirements as put forth in the beginning of this thread....

Do help....


Tuesday, February 10, 2009 8:14 AM

Hi Subumysore,

Then what  result you want to get when it goes to another page?
Do you want to sign in without using its default way for you to login?

Best Regards

Yichun Feng

Please remember to mark the replies as answers if they help and unmark them if they provide no help.