[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.6

Visual Basic 2008 using webbrowser to Log into a website (pass userName and password to the textboxes and submit it)

Asked by Jack_Knight in Microsoft Visual Studio Express, Hypertext Markup Language (HTML), Microsoft

I am trying to navigate to a website and enter in my userName and password and submit it. But the code only opens up the website and enters in only the username not the password or the submit button?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
Public Class boabrowsr1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("http://mywebsite")
        WebBrowser1.Show()
           End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
         fill("remote", "Mypassword", "text")
         fill("password","myPassword","Password")
        Me.submitButton(" Submit")
 
    End Sub
 
Private Sub fill(ByVal textboxname As String, ByVal text As String, ByVal type As String)
 
 
        Dim doc2 As mshtml.HTMLWindow2Class = CType(WebBrowser1.Document.Window.DomWindow, mshtml.HTMLWindow2Class)
        Dim objDoc2 As mshtml.IHTMLDocument = CType(doc2.document, mshtml.IHTMLDocument)
 
 
        ' Create a collection of all the "input" elements in the page.
        Dim wbrAll As mshtml.IHTMLElementCollection = objDoc2.getElementsByTagName("input")
        ' Create an object that will be a single instance of an input element
        Dim wbrElm As mshtml.IHTMLElement
 
        ' Create a few variable to hold values from the input element
        Dim strName2 As String
        Dim strId2 As String
        Dim strvalue2 As String
        Dim strType2 As String
 
        Try
            ' Iterate through the collection of html elements, i.e. our input collection
            For Each wbrElm In wbrAll
 
                ' Assign the inner html values of the input to our variables
 
                strName2 = wbrElm.getAttribute("name")
                strId2 = wbrElm.id
                strvalue2 = wbrElm.innerText
                strType2 = wbrElm.getAttribute("type")
 
                ' We are only interested in filling text boxes, and only interested in a specific  one, i.e. "q"
                If strType2.ToString.ToLower = type And strName2.ToString = textboxname Then
                    ' Set the "value" with the setAttribute method.
 
                    wbrElm.setAttribute("value", text)
                    ' Scroll the page so that we can see the input is filled.
                    'wbrElm.scrollIntoView()
 
                End If
 
            Next
        Catch ex As Exception
            '    ' If there is an error, it is for the reason stated below - you can change the code to be more robust or to handle different pages. 
            '    MessageBox.Show("It is likely that your input does not exist or has no name attribute. Check the HTML source.", "No name att. or no input available", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
 
    End Sub
 
Sub submitButton(ByVal txtButtonName As String)
        Dim doc As mshtml.HTMLWindow2Class = CType(WebBrowser1.Document.Window.DomWindow, mshtml.HTMLWindow2Class)
        Dim objDoc As mshtml.HTMLDocument = CType(doc.document, mshtml.HTMLDocument)
 
           Dim wbrAll As mshtml.IHTMLElementCollection = objDoc.getElementsByTagName("input")
        ' Create an object that will be a single instance of an input element
        Dim wbrElm As mshtml.IHTMLElement
 
        ' Create a few variables to hold values from the input element - you can create more
        Dim strName As String
        Dim strId As String
        Dim strValue As String
        'Dim strType As String
 
        'Try
        ' Iterate through the collection of html elements, i.e. our input collection
        For Each wbrElm In wbrAll
            'MessageBox.Show(wbrElm.getAttribute("id"))
            ' Assign the inner html values of the input to our variables
            strName = wbrElm.getAttribute("id") & ""
            strId = wbrElm.id
            strValue = wbrElm.innerText
            'strType = wbrElm.getAttribute("type")
            'MessageBox.Show(strName.ToString, txtButtonName)
            ' We are only interested in submitting the form right now, so, this time around we'll do the normal submit button
            If strName = txtButtonName Then
 
                ' Scroll the page so that we can see button that we are clicking - totally not necessary - but makes the user thing something is happening - kind of like lights on early mainframes
                'wbrElm.scrollIntoView()
 
                ' Create an object to access the DOM and get to the submit button
                Dim btnSearch As mshtml.HTMLButtonElement
                ' Set the created search button equal to the search button on the google page, and set it to the first one (i.e. the one at the top of the page (there are 2), to the index is 0
                btnSearch = objDoc.all.item(txtButtonName, 0)
                ' Click the button
                btnSearch.click()
 
                ' Remember that there are more elements that you can create, like htmlInputImage. HTMLAnchorElement and many more
 
            End If
 
        Next  ' end interation through html oject collection
        'Catch ex As Exception
        ' If there is an error, it is for the reason stated below - you can change the code to be more robust or to handle different pages. 
        '  MessageBox.Show("It is likely that your submit does not exist or has no name attribute. Check the HTML source.", "No name att. or no submit available", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
 
        ' End Try
 
 
 
    End Sub
End Class
 
Related Solutions
Keywords: Visual Basic 2008 using webbrows…
 
Loading Advertisement...
 
[+][-]03/11/09 01:22 PM, ID: 23861912Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/12/09 02:45 PM, ID: 23873999Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]03/12/09 02:45 PM, ID: 23874005Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]03/12/09 03:07 PM, ID: 23874247Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Microsoft Visual Studio Express, Hypertext Markup Language (HTML), Microsoft
Sign Up Now!
Solution Provided By: TheLearnedOne
Participating Experts: 1
Solution Grade: A
 
 
Loading Advertisement...
20091021-EE-VQP-81 - Hierarchy / EE_QW_3_20080625