[VB .net / VBA] Get elements DOM XHTML

WEB, Hardware, Software e generali

Moderatore: MODERATORE

Avatar utente
Rombo di Tuono
Site Admin
Messaggi: 6963
Iscritto il: 04 lug 2007, 18:07
Località: Seconda stella a DESTRA...

[VB .net / VBA] Get elements DOM XHTML

Messaggio da Rombo di Tuono »

[bannerizza]x[/bannerizza] [adsensa]x[/adsensa]
For my future reference and to help other people banging their head on this... :D

PROBLEM: access an HTML element and change its value with visual basic

TWIST: if the element has an unique value for attribute "name", you may think to access directly that element by its name.


GetElementByName doesn't work (or doesn't exist at all) : pay attention to the singular "ElementBy"

GetElementsByName works, BUT IT RETURNS A COLLECTION of objects. pay attention to the plural "ElementsBy"

If you use it this way: obj1=GetElementsByName ("name"), it will end in a obj1 that is a collection, and then it has NOT the "value" property or attribute

So, the correct way to access an element by its unique name is this:

1) Loop For to get all elements by tag name
2) If statement to place an action to a single element

Codice: Seleziona tutto

For Each objLink In IE.Document.GetElementsByTagName("input")
        If objLink.Name = "nGiust0" Then
            objLink.Value = "1"
            Exit For
        End If
Next objLink
"Exit For" is good, because I know that name is unique

If anyone has a better way, please tell me :D