Search code examples
htmlautoit

Get HTML element by attribute


I'm using AutoIt to parse HTML. I want to get all HTML elements by an attribute's value. Example:

<div data-source="xxx">The div content XXX</div>
<div data-source="zzz">The div content of ZZZ</div>

The div -element containing the attribute-value pair data-source="xxx" should be selected.


Solution

  • Try this?

    $ohtml = ObjCreate('HTMLFILE')
    $ohtml.body.innerHTML = '<div data-source="xxx">The div content XXX</div>' & @CRLF & _
                            '<div data-source="zzz">The div content of ZZZ</div>'    
    
    Dim $selected_node
    For $div in $ohtml.body.getElementsByTagName("div")
        If $div.getAttribute("data-source") = 'xxx' Then
            $selected_node = $div
            ExitLoop
        EndIf
    Next
    
    ConsoleWrite($selected_node.innerHTML & @CRLF)