Results 1 to 11 of 11

Thread: [RESOLVED] Website login with WebBrowser and VB.Net 2010

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Resolved [RESOLVED] Website login with WebBrowser and VB.Net 2010

    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?

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Website login with WebBrowser and VB.Net 2010

    and there are no errors raised
    Odd .... oh, wait ...

    wbc.ScriptErrorsSuppressed = True
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Re: Website login with WebBrowser and VB.Net 2010

    There are also no errors raised when the "ScriptErrorsSuppressed = True" line is removed.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Re: Website login with WebBrowser and VB.Net 2010

    When I immediately run the code a second time, it raises err #91 ("Object reference not set to an instance of an object.") at the following line:

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

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Website login with WebBrowser and VB.Net 2010

    Ok then. Had to check obviously. Are you sure that this button has a 'click' to invoke? It's not 'onclick', for example? Or a simple anchor? Have you tried double clicking (I've found that some sites absorb the first click in focussing on the button), possibly with a delay in the middle? Have you tried with the browser visible before the log-in? Have you done the process up to the click and then manually clicked to see that it works?

    What I'm trying to say her is that it's a very inexact science and if push comes to shove your best bet may just be to position the cursor over the button and create a mouse click. This process above all others is the one that puts the trial in trial and error!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Website login with WebBrowser and VB.Net 2010

    Oh, and this ....

    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
    ... is not good at all (and not just because you never appear to stop the watch!!!!!)
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Re: Website login with WebBrowser and VB.Net 2010

    Good catch on the timer...I should have a timer.Stop() before my Exit For statement!

    As for the "click" vs. "onclick" events, I'm pretty sure that its a "click" event because my old VBA code (that still works) uses the "click" event. You can see this in the VBA code that I included in my initial post.

    The line from the webpage's source HTML is (I'm trying to automatically log into the VBForums website page):

    Code:
    <input type="submit" class="loginbutton" tabindex="104" value="Log in" title="Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself." accesskey="s" />
    What don't you like about my "Application.DoEvents()" approach? Can you suggest an alternate approach?

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Re: Website login with WebBrowser and VB.Net 2010

    dunfiddlin, I appreciate your help here. You suggested several trial and error ideas to investigate the problem. A few of them require that the WebBrowser be visible. Even though it is not visible at any point when the code runs, "?wbc.visible" equals TRUE in the Immediate Window. Like I said, I'm really stumped!

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Website login with WebBrowser and VB.Net 2010

    VBA .Click and VB.Net HTML Element InvokeMember are not at all the same thing unfortunately. T'would be folly to assume that they are equivalent.

    In the meantime if you want to see grown men crying, gnashing their teeth, and generally blasting demons with extreme prejudice, just enter Application.DoEvents() in the search box! It's not pretty.

    We've also has endless discussions about how to ensure that a webpage is fully loaded with less than spectacular results so you may care to look those up too. These days I use the Awesomium browser in my applications as it's loaded document tracking seems to be considerably superior to the standard control.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Re: Website login with WebBrowser and VB.Net 2010

    Thanks for the tip on the Awesomium browser, I'll look into it.

    I've attached an image that shows that VB.Net thinks that the WebBrowser is visible during the execution of the program just so you don't think I've completely lost my mind .

    Name:  5-7-2013 4-42-41 PM.png
Views: 5868
Size:  21.1 KB

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Re: Website login with WebBrowser and VB.Net 2010

    I think I have figured out what is the issue. In my VBA code (Microsoft Access), I was opening an instance of the Internet Explorer via the CreateObject("InternetExplorer.Application") statement. This instance of IE was separate from MS Access so after the website login was completed, I could work in the website regardless of whether or not my Access VBA application was still running. In my VB.Net code, I was creating a WebBrowser control within the VB.Net application itself, but I had not added the WebBrowser control to my VB.Net form. So when I used the WebBrowser control to navigate to a website, it performed the navigation but since the WebBrowser control was not on a form I couldn't see the webpage it navigated to.

    My solution to this issue was to use the CreateObject("InternetExplorer.Application") statement in my VB.Net code to create an instance of IE that is separate from my VB.NET application. That way, my application can automatically log into the website and then close itself without also closing IE and the webpage.

    If there are better ways to do this, instead of the CreateObject("InternetExplorer.Application") statement, I'd like to hear about them.
    Last edited by Mark@SF; May 10th, 2013 at 10:26 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width