PDA

View Full Version : Accessing data in getelementbyid() model



Rhett
11-18-2012, 04:21 PM
Hi, I am having trouble accessing the information in the "menu 2" part of my code below. If I add a watch to IE.document.getelementbyid("menu2").all I can see that this object contains the following:

+constructor
ie8_length
length
+Item 1
+Item 2
+Item 3
+Item 4
+Item 5
+Item 6

If I click on Item 6, the list expands and there is an item called ie8_href that contains a url I would like to access. However, the method I am trying to access this data (shown below as notes in the code) gives me an "Object doesnt support this property or method" error. Can anyone please help me out? I have been struggling with this way too long.:banghead:


Sub Test()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.application")
IE.Visible = True
IE.navigate ("MyWebsite")
While IE.Busy Or IE.readyState <> 4: DoEvents: Wend

IE.document.getelementbyid ("menu2")
'IE.document.getelementbyid("menu2").all("Item 6").href


While IE.Busy Or IE.readyState <> 4: DoEvents: Wend
Debug.Print IE.LocationURL
End Sub

Crocus Crow
11-19-2012, 04:32 AM
A few things to note:

1. getElementById returns a HTML element (object) so you could assign its return value to an object variable, or a suitably declared HTML data type:

Dim menu2 As Object
Set menu2 = IE.document.getElementById("menu2")
'Do something with menu2 properties or methods

Or directly invoke the methods of IE.document.getElementById("menu2") or read/write its properties.

2. I don't think .all("Item 6") can reference the required element in the .all() array/collection, which is normally referenced by index number or the element's name attribute string:

Dim obj As Object
Set obj = IE.document.getelementbyid("menu2").all(5) '6th item
Set obj = IE.document.getelementbyid("menu2").all("name_attribute")

3. The href property is a string, so you assign it to a string variable, or otherwise do string operations on it:

Dim theHref As String
theHref = IE.document.getelementbyid("menu2").all(5).href

If you still need help please post the relevant HTML.

Rhett
11-19-2012, 09:00 PM
Thanks!:clap:
Below was exactly what I needed. Amazing how you can get hung up on a little thing like that .

IE.document.getelementbyid("menu2").all(5).href