I have this piece of code that basically should click on several buttons from a website successively.
For Each Element In Ie.getElementsByTagName("a")
If Element.innerText = "Advanced Search" Then
'Debug.Print element.innerText
'**element.Click**
Exit For
Else
End If
Next Element
Set objShell2 = CreateObject("Shell.Application")
IE_count = objShell2.Windows.Count
For X = 0 To (IE_count - 1)
On Error Resume Next
my_url = objShell2.Windows(X).Document.Location
my_title = objShell2.Windows(X).Document.Title
MsgBox ("The title of this page is: " & my_title)
If my_title Like "Client List Management" & "*" Then
Set ie2 = objShell2.Windows(X).Document
my_title3 = ie2.Title
MsgBox ("The title of Client List Management is: " & my_title3)
Exit For
Else
End If
Next
For Each Element In ie2.getElementsByTagName("a")
If Element.innerText = "Accounts" Then
'Debug.Print element.innerText
'**element.Click**
Exit For
Else
End If
Next
For Each Element In ie2.getElementsByTagName("a")
If Element.innerText = "SEARCH" Then
'Debug.Print element.innerText
'**element.Click**
Exit For
Else
End If
Next
For Each Element In ie2.getElementsByTagName("a")
If Element.innerText = "BATCH EXPORT" Then
'Debug.Print element.innerText
'**element.Click**
Exit For
Else
End If
Next
The problem is that when I run the whole macro, only the first button is being clicked (button "Advanced Search") and nothing happens with the others that should be clicked after the "Advanced Search" button is clicked.
However, when I try to debug my code and step into every step of the program, each and every button that should be clicked is being clicked. Everything works perfectly when I debut. The only problem resides when the program is actually running by its own.
Do you know how to fix this problem?
Thank you :)
If timing is an issue then you can wait for Internet Explorer to not be busy and the readystate
is equal to 4.
Try placing While Ie.readyState <> 4 Or Ie.Busy: DoEvents: Wend
in sections where the page might be loading. (after navigation, button clicks, etc.).
If all said that this isn't sufficient enough, you could always play with the sleep API.
#If VBA7 And Win64 Then
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If
I would only do this if you can't get the wait above to work.
Readystate 4 means that the request had been sent, the server had finished returning the response and the browser had finished downloading the response content.
Busy from Microsofts Documentation is described as:
Gets a value that indicates whether the object is engaged in a navigation or downloading operation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.