• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

getElementById only if it exists

Using some vb code to extract some information out of a website
Occasionally the website goes down, and presents a 504 error page.

The element I'm trying to extract is not present on the 504 page.
How can I edit my code so it only extracts the information if it actually exists?

Thanks in advance

Set TDelements = HTMLdoc.getElementById("menu_content").getElementsByTagName("TD")

Open in new window

0
antonioking
Asked:
antonioking
2 Solutions
 
rwniceingCommented:
Probably you want it on VBA on Excel, Right ? if so, test following code
to check object ID first before any info extraction

set  obj= HTMLdoc.getElementById("menu_content")

If obj Is Nothing Then
debug.print "obj not exist"
Else
debug.print "obj exist"
End If

Open in new window

0
 
MlandaTCommented:
Set MenuElement = HTMLdoc.getElementById("menu_content")

If MenuElement Is Nothing Then
        Set TDelements = Nothing
Else
        Set TDelements = MenuElement.getElementsByTagName("TD")
End If

'Make sure to check TDElements is not nothing before you use it
If Not TDElements Is Nothing Then

       'Do your thing here.

End If

Open in new window

However, I think the best is for you to check for the 504 when you load HTMLdoc. That way, you don't even go as far as trying to parse the HTML soon as you realise that the site is down. It is however still good practice to do lots of error checking and try to avoid making assumptions that (for example) the menu_element will always be there.
0
 
antoniokingAuthor Commented:
Thank you both for your help
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now