I gave 2 examples of how to change the code so the clicks were registered. With <span> set to the attribute, the text within the span needs to be clicked. Otherwise, clicking the image wouldn't do anything. So one option would be to set all the <img> elements to id=#. However, that would leave out the <span> text. We could set them both programmatically, without touching the html source file:
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
Opt("GUIOnEventMode", 1)
Opt("GUICloseOnESC",1)
Global $MainWindow = GUICreate("Main Window", 833, 822, 335, 214)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEMain", $MainWindow)
$oIE = _IECreateEmbedded ()
GUICtrlCreateObj($oIE, 10, 10, 500, 400)
_IENavigate($oIE, @ScriptDir&"/images/index.html")
;_IEDocWriteHTML($oIE,fileread(@ScriptDir&"/images/index.html")); "<html><body><button id=""BTN"">Press This</button><br><br><button id=""BTk"">Press This</button></body></html>")
_IELoadWait($oIE)
; Loop through and set IMG and SPAN elements to have the id attribute of their parent Anchor element
Local $oDOMElem
; Loop through the elements with attributes id=1 to id=4
For $i = 1 to 4
$oDOMElem = $oIE.document.getElementById($i)
If IsObj($oDOMElem) Then
$oDOMElem = $oDOMElem.firstChild
If IsObj($oDOMElem) Then
While 1
ConsoleWrite("Child node of id '" & $i & "': type = " & $oDOMElem.nodeType & ", name = " & $oDOMElem.nodeName & @LF)
; Node type of 1 = element
If $oDOMElem.nodeType = 1 Then
; If child is <IMG> or <SPAN> element, set the id attribute in each
; (note, in general, this is bad practice - usually only set one element to have an id attribute!)
Switch $oDOMElem.nodeName
Case "IMG", "SPAN"
$oDOMElem.setAttribute("id", $i)
EndSwitch
EndIf
$oDOMElem = $oDOMElem.nextSibling
If $oDOMElem = Null Then ExitLoop
;If Not IsObj($oDOMElem) Then ExitLoop ; alternate
WEnd
EndIf
EndIf
Next
Local $oEventsDoc = ObjEvent($oIE.document, "Event_", "HTMLDocumentEvents2")
#forceref $oEventsDoc
; _IEErrorHandlerRegister("MyErrFunc")
GUISetState(@SW_SHOW, $MainWindow) ; will display an empty dialog box
While 1
Sleep(1000) ; Idle around
WEnd
Exit
;-- Open new window when button in 'IE' pressed
volatile Func Event_onclick($oEvtObj)
If IsObj($oEvtObj) Then
Switch $oEvtObj.srcElement.id
Case 0
ConsoleWrite("Click not associated with any DOM element with an id attribute" & @LF)
Case "1"
ConsoleWrite("UPLOAD"&@LF)
Case "2"
ConsoleWrite("DOWNLOAD"&@LF)
Case "3"
ConsoleWrite("ABOUT"&@LF)
Case "4"
Exit
Case Else
ConsoleWrite("Click of element id '" & $oEvtObj.srcElement.id & "' received" & @LF)
EndSwitch
EndIf
EndFunc
Func CLOSEMain()
Exit
endfunc
However, I'm wondering now if there's an event we aren't capturing properly, as replacing the 'href' attribute with an actual website will cause the page to change without actually registering a specific DOM element being clicked. Perhaps we need to revisit HTMLAnchorEvents..