Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27513

VS 2010 Website login with WebBrowser and VB.Net 2010

$
0
0
I am converting an old Access database application over to VB.Net and have run into an issue that I need some help with. In the Access (VBA) application I can automatically login to various websites, but I can't get this to work in VB.Net. In the VB.Net code I am using a WebBrowser to navigate to a website.

Here is the my old VBA code (that works like a charm):

Code:

  Set objIE = CreateObject("InternetExplorer.Application")

  StatusBar "Opening the login window, please wait..."
  objIE.navigate (strURL)
  Do While objIE.busy
    '...wait for IE to load page
  Loop
 
  Set objHTMLDoc = objIE.Document
   
  '...wait until the key node is visible
  flgOK = False: lngStart = Timer
  Do While flgOK = False
    For Each objInput In objHTMLDoc.all.tags("INPUT")
      Debug.Print objInput.nodeName, objInput.Name, objInput.Value
      If objInput.Value = "User Name" Then
        flgOK = True
        Exit For
      End If
    Next objInput
    If (Timer - lngStart) > 15 Then Exit Do  '...quit after 15 seconds if search text hasn't been found
  Loop

  '...complete site login information
  StatusBar "Completing login procedure, please wait..."
  If flgOK = True Then
    For Each objInput In objHTMLDoc.all.tags("INPUT")
      If objInput.Name = "vb_login_username" Then
        objInput.Value = strUN
      ElseIf objInput.Name = "vb_login_password" Then
        objInput.Value = strPW
      ElseIf objInput.Value = "Log in" Then
        objInput.Click
        MsgBox "Login complete.", vbOKOnly + vbInformation
        objIE.Visible = True
        Exit For
      End If
    Next objInput
  Else
    MsgBox "Cannot locate key input node.", vbOKOnly + vbCritical
  End If

Here is the my VB.Net code (that I'm having trouble with):

Code:

            wbc.ScriptErrorsSuppressed = True
            wbc.Navigate(strURL)
            timer = Stopwatch.StartNew
            flgTimedOut = False
            Do Until wbc.ReadyState = WebBrowserReadyState.Complete
                If timer.Elapsed.Seconds > intTimeOut Then
                    flgTimedOut = True
                    Exit Do
                End If
                Application.DoEvents()
            Loop

            If (wbc.Document IsNot Nothing) Then

                wbc.Document.GetElementById("navbar_username").SetAttribute("Value", strUN)
                wbc.Document.GetElementById("navbar_password").SetAttribute("Value", strPW)

                'the "Log in" button does not have an ID, so find it by its TabIndex and OuterHTML values
                For i As Integer = 0 To wbc.Document.GetElementsByTagName("Input").Count
                    With wbc.Document.GetElementsByTagName("Input").Item(i)
                        Debug.WriteLine("Input #" & i & ": " & .Name & ", TabIndex = " & .TabIndex)
                        Debug.WriteLine(.OuterHtml)
                        If .TabIndex = 104 And InStr(.OuterHtml, "Enter your username and password") <> 0 Then
                            .InvokeMember("click")
                            wbc.Visible = True
                            If Not flgSilent Then
                                MessageBox.Show("Operation complete (" & timer.Elapsed.ToString("hh\:mm\:ss") & ").", _
                                                "VBForums Login", MessageBoxButtons.OK, MessageBoxIcon.Information)
                            End If
                            Exit For
                        End If
                    End With
                Next

            Else
                MessageBox.Show("Browser navigation did not complete in the alloted time (" & intTimeOut.ToString & _
                                If(intTimeOut = 1, " second", " seconds") & ").", "VBForums Login", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            End If '(wbc.Document IsNot Nothing)

When I single-step through the code I can see that the "strUN" and "strPW" values are being assigned to the navbar_username and navbar_password elements and the InvokeMember("click") is happening. The "Operation complete" MessageBox appears and there are no errors raised. Having said that, the webpage never appears. The Windows Task Manager never shows an instance of Internet Explorer (iexplore.exe) in its Processes tab page.

I'm stumped. Can anyone give me some advice?

Viewing all articles
Browse latest Browse all 27513

Trending Articles