Hi,
So the below is supposed to OPEN internet explorer, WAIT UNTIL IT LOADS, add username & password and click login, THEN WAIT AGAIN UNTIL THE PAGE LOADS and then many other clicks on hyperlinks, but up to here is enough to show my issue.
So far I've been using "Application.Wait Now + TimeValue("00:00:05")", but honestly it is not very helpful, because sometimes the page takes 1 second to load and other times it just randomly takes like 15 seconds(so macro gives error). I'm talking about hundreds of clicks I have to do on this website. If I could make the macro do the next click exactly when the page stops loading it would be the best.
It's probably an obvious solution, but I just fail to see it. I'm guessing it is my variables(the IE one), but I'm not sure how to fix it.
Could you please let me know what exactly I should change on the 2nd "Do While IE.readyState <> READYSTATE_COMPLETE Loop" on the below macro so it actually waits before continuing with the next lines?
I know for a fact it is not waiting for the loading to finish because the "Debug.Print HTMLA.getAttribute("href")" is giving me the "href"s from the login page(first page) instead of waiting and giving me the "href"s of the 2nd page.
Thank you very much in advance!
P.S. For obvious reasons, I have changed the URL to a random one(Google) and the username and password to simply username and password.
P.P.S. Most of the below was from checking WiseOwlTutorials. I'm still a beginner with VBA.
So the below is supposed to OPEN internet explorer, WAIT UNTIL IT LOADS, add username & password and click login, THEN WAIT AGAIN UNTIL THE PAGE LOADS and then many other clicks on hyperlinks, but up to here is enough to show my issue.
So far I've been using "Application.Wait Now + TimeValue("00:00:05")", but honestly it is not very helpful, because sometimes the page takes 1 second to load and other times it just randomly takes like 15 seconds(so macro gives error). I'm talking about hundreds of clicks I have to do on this website. If I could make the macro do the next click exactly when the page stops loading it would be the best.
It's probably an obvious solution, but I just fail to see it. I'm guessing it is my variables(the IE one), but I'm not sure how to fix it.
Could you please let me know what exactly I should change on the 2nd "Do While IE.readyState <> READYSTATE_COMPLETE Loop" on the below macro so it actually waits before continuing with the next lines?
I know for a fact it is not waiting for the loading to finish because the "Debug.Print HTMLA.getAttribute("href")" is giving me the "href"s from the login page(first page) instead of waiting and giving me the "href"s of the 2nd page.
Thank you very much in advance!
P.S. For obvious reasons, I have changed the URL to a random one(Google) and the username and password to simply username and password.
P.P.S. Most of the below was from checking WiseOwlTutorials. I'm still a beginner with VBA.
VBA Code:
Option Explicit
Sub GetData()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLAs As MSHTML.IHTMLElementCollection
Dim HTMLA As MSHTML.IHTMLElement
'Open Internet Explorer
IE.Visible = True
IE.navigate "Google.com"
Do While IE.readyState <> READYSTATE_COMPLETE [B]' This one works[/B]
Loop
'Add Username and Password
IE.document.forms("loginForm").elements("UserName").Value = "username"
IE.document.forms("loginForm").elements("Password").Value = "Password"
'Click on Login Button
Set HTMLDoc = IE.document
Set HTMLInput = HTMLDoc.getElementById("submitButton")
HTMLInput.Click
Do While IE.readyState <> READYSTATE_COMPLETE [B]'This one DOES NOT work. I've been using "Application.Wait Now + TimeValue("00:00:05")" in here as an alternative[/B]
Loop
'Find carrier Portal href hyperlink
Set HTMLAs = HTMLDoc.getElementsByTagName("a")
For Each HTMLA In HTMLAs
Debug.Print HTMLA.getAttribute("href")
Next HTMLA
End Sub