Internet Automation Retrieving DIV class results

mdo8105

Board Regular
Joined
Nov 13, 2015
Messages
83
Hi everyone,

I am running into issues retrieving data back into excel after I automate the click on the web page. The html code that I believe has the results is
HTML:
div class="zipcode-result-address">                                                                                        123 FAKE ST
                                            CHULA VISTA CA [B]91910-3885[/B]
                                        
The code is able to populate the webpage and search and I am not receiving an error as well, I'm just not getting anything back.
[CODE]Sub Atest()Dim objIE As InternetExplorer
Dim aEle As HTMLDivElement
Dim Result As String
Dim y As Integer


'innitiating a new instance of IE and assiging it to objIE
Set objIE = New InternetExplorer


'make IE browser visible
objIE.Visible = True


'navigate IE to this web page
objIE.navigate "https://tools.usps.com/zip-code-lookup.htm?byaddress"


'wait here a few seconds whilte the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop


'enter the values in the fields
'Address
objIE.document.getElementsByName("tAddress").Item(0).Value = ThisWorkbook.Worksheets("Address Verification").Cells(5, 2).Value
'Zip
objIE.document.getElementsByName("tZip-byaddress").Item(0).Value = ThisWorkbook.Worksheets("Address Verification").Cells(5, 5).Value
'Click the find button
objIE.document.getElementById("zip-by-address").Click
'wait again for the browswer
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop


'For each  element in the collection of bjects with class of '"zipcode-result-address"
For Each elm In objIE.document.getElementsByClassName("zipcode-result-address")
            MsgBox elm.innerText
         
        Next elm
End Sub[/CODE]
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I am able to find the value via a Msgbox with this statement
Code:
Set Nodelist = objIE.document.getElementsByTagName("p")For Each Elem In Nodelist
MsgBox Elem.innerText


Next Elem
But I cannot find out which paragraph position it is and when I try to out all of the text onto a sheet nothing comes out, it as if I can only view it in MsgBox.
 
Upvote 0
The first code you posted works fine for me and returns 128 addresses via the message box.

Mind you I did have a a breakpoint on the For Each meaning that the page had a little more time to load, which indicates this could be a timing issue.

Perhaps you could try adding this, which should delay things until the actual document is loaded.
Code:
While objIE.document.ReadyState <> "complete": DoEvents: Wend
 
Last edited:
Upvote 0
Hi Norie,

Thank you for the response. I updated the Do while statement to While and Wend; however, still nothing is coming across, not even an error. I am using IE 11. Do you think that is causing the issue?
 
Upvote 0
If you step through the code, letting the page load, does the code work?
 
Upvote 0
Hi Norie,

It works like a charm when I step through. Do you think the browser is timing out?
 
Upvote 0
Try this as the 'wait' before the For Each loop:
Code:
While objIE.document.getElementsByClassName("zipcode-result-address").Length = 0 
    DoEvents
Wend
Of course, if there is more than 1 element with the class name "zipcode-result-address" the above code could still cause a timing issue if the 2nd and subsequent elements aren't ready in the For Each loop.
 
Last edited:
Upvote 0
It's working in an interesting way. It will finish the code and works properly once I stop the page from loading in IE. It's looping here:
Code:
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
 
Upvote 0
Not ideal but you could try using Application.Wait to, well, wait until the page loads instead of your current loops.
 
Upvote 0
Hi Norie,

So far it is working. I'm wondering if I was just having poor internet connection at the moment. Thank you so much.
 
Upvote 0

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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