Standard loops to wait for IE webpage to load not working

lukez

Board Regular
Joined
Nov 6, 2014
Messages
56
Hey all
I'm left scratching my head. My stock broker is moving to a new website and I have to redo my excel macro to login and get some information from the new website.
I can't seem to find any code that will wait until the page/login form finishes loading.
Any ideas?
I've tried a bunch of variations of loops such as the ones below but nothing wants to wait for the page to finish loading.
Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop


Here is my code so far:
VBA Code:
Sub login()
    Dim IE As Object
    Dim objElement As Object
    Dim objCollection, objCollection2 As Object
    Dim n as Long


    ' Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")

    IE.Visible = True
 
    IE.Navigate "https://client.bnc.ca/nbdb/login"
 
    ' Wait while IE loading...
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop
 
     n = 0

    For Each itm In IE.document.all
        If itm = "[object HTMLInputElement]" Then
        n = n + 1
            If n = 1 Then
                itm.Focus             'Activates the Input box (makes the cursor appear)
                itm.Value = "test"
            End If
        End If
    Next
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Quick and dirty method.
VBA Code:
Sub login()
    Dim IE As Object
    Dim inputElement As Object

    ' Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")

    IE.Visible = True
 
    IE.Navigate "https://client.bnc.ca/nbdb/login"
    
    On Error Resume Next
    Do 'should add a time out
        DoEvents
        Set inputElement = IE.Document.getElementById("username")
    Loop Until Not inputElement Is Nothing
    On Error GoTo 0
    
    inputElement.Value = "test"
End Sub
 
Upvote 0
Quick and dirty method.
VBA Code:
Sub login()
    Dim IE As Object
    Dim inputElement As Object

    ' Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")

    IE.Visible = True

    IE.Navigate "https://client.bnc.ca/nbdb/login"
   
    On Error Resume Next
    Do 'should add a time out
        DoEvents
        Set inputElement = IE.Document.getElementById("username")
    Loop Until Not inputElement Is Nothing
    On Error GoTo 0
   
    inputElement.Value = "test"
End Sub
Thanks for the quick response but it gets stuck in the DO loop. I've tried a similar loop and had the same issue. Does it not get stuck in the loop for you?
 
Upvote 0
I also loose communication to IE in the loop, for example I added a timeout and when it times out these don't do anything:
IE.Quit
Set IE = Nothing
 
Upvote 0
I just copied and pasted the example above and it worked fine on my end. I don't know why it's not working. You could set a reference to Microsoft Internet Controls and perhaps use some of IE's events such as downloadComplete or navigateComplete. Let me know if you make any progress.

1583436909899.png
 
Upvote 0
I just copied and pasted the example above and it worked fine on my end. I don't know why it's not working. You could set a reference to Microsoft Internet Controls and perhaps use some of IE's events such as downloadComplete or navigateComplete. Let me know if you make any progress.

View attachment 8325
Thanks for the confirmation that it works, so it's obviously my system.
I installed a fresh version on Excel 2010 (was on 2016 64-bit). - still behaved the same
Installed IE 11 again - still behaved the same
Added all the references related to IE - still behaved the same
Changed code to use Dim IE As InternetExplorerMedium and set IE = new InternetExplorerMedium - THIS NOW WORKS!
I don't get it. So frustrating.

So I continued with developing the code for this main login page and got the password in and clicked the submit button but keep getting incorrect username/pass using the macro. It works if I type it in instead.
Yes I double checked the username and pass is correct and there are no phantom spaces.
If I run the code and stop it before I hit the submit button and I delete and type the last letter of each the username and password and hit submit it works.
It is like the webpage doesn't recognize what's in the form box when you use inputElement.Value =
I tried using focus as well before the .value and didn't help.
 
Upvote 0
You're already beyond me in this sort of thing. I suppose that you could use sendkeys to send the last char of username and pw. Don't know.
 
Upvote 0
Pretty dirty but I used sendkeys for the username and password with some application waits and got it to go.
If someone has a better way to do this please let me know, here is the code that works:

VBA Code:
Sub login()
    Dim btn As Object
    Dim objElement As Object
    Dim objCollection, objCollection2 As Object
    Dim IE As InternetExplorerMedium
    Dim inputElement As Object

    ' Create InternetExplorer Object
    Set IE = New InternetExplorerMedium

 
    ' You can uncoment Next line To see form results
    IE.Visible = True
 
    ' Send the form data To URL As POST binary request
    IE.Navigate "https://client.bnc.ca/nbdb/login"
    
    timeout = DateAdd("s", 6, Now)
    On Error Resume Next
    Do 'should add a time out
        If Now > timeout Then
            IE.Quit
            Set IE = Nothing
            Exit Sub
        End If
        DoEvents
        Set inputElement = IE.Document.getElementById("username")
    Loop Until Not inputElement Is Nothing
    On Error GoTo 0
    Application.Wait DateAdd("s", 1, Now)
    inputElement.Focus
    Application.SendKeys "Test"
    Application.Wait DateAdd("s", 1, Now)
    
    Set inputElement = Nothing
    
    On Error Resume Next
    Do 'should add a time out
        DoEvents
        Set inputElement = IE.Document.getElementById("password-hidden")
    Loop Until Not inputElement Is Nothing
    On Error GoTo 0
 
    inputElement.Focus
    Application.SendKeys "Test"
    Application.Wait DateAdd("s", 1, Now)
    Set inputElement = Nothing


Set ElementCol = IE.Document.getElementsByClassName("or-big mat-button mat-button-base")

 For Each btninput In ElementCol
    btninput.Focus
    btninput.Click
 Next btninput
    

        
    'IE.Quit
    'Set IE = Nothing
    'Set objElement = Nothing
    'Set objCollection = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top