Cycle through HTML elements

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
rynsp8
Posts: 46
Joined: 30 Jan 2018, 12:45

Cycle through HTML elements

18 Jul 2018, 08:46

I need some help cycling through HTML elements.

I have a script that grabs 5 different items from within a 91 page report. These items exist within an iFrame, on different pages, and I've been able to successfully grab these items statically, however, I need to add a piece of logic that will iterate through the <pre> HTML elements on the pages to select the correct element with the innerHTML that I'm looking for.

I have no idea really how to start.

Thanks for any help.
rynsp8
Posts: 46
Joined: 30 Jan 2018, 12:45

Re: Cycle through HTML elements

18 Jul 2018, 10:09

Alright,

I thought I'd attempt the below script. However, I'm getting an error: "0x80020003 - Member not found" Which is referencing my line of code attempting to return the .length of the <pre> elements.

Any thoughts?

[code}
#Persistent
#SingleInstance Force
#NoEnv
;*****************************************************************************

pwb := WBGet()


;pwb.document.parentWindow.frames("FrameB").document.all.tags("pre").length -----> will return the amount of Pre elements on a page within JS console in IE.

a:= pwb.document.parentWindow.frames("FrameB").document.all.tags("pre").length


Msgbox % a

Return





;************Pointer to Open IE Window******************
WBGet(WinTitle="ahk_class IEFrame", Svr#=1) { ;// based on ComObjQuery docs
static msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT")
, IID := "{0002DF05-0000-0000-C000-000000000046}" ;// IID_IWebBrowserApp
;// , IID := "{332C4427-26CB-11D0-B483-00C04FD90119}" ;// IID_IHTMLWindow2
SendMessage msg, 0, 0, Internet Explorer_Server%Svr#%, %WinTitle%

if (ErrorLevel != "FAIL") {
lResult:=ErrorLevel, VarSetCapacity(GUID,16,0)
if DllCall("ole32\CLSIDFromString", "wstr","{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr",&GUID) >= 0 {
DllCall("oleacc\ObjectFromLresult", "ptr",lResult, "ptr",&GUID, "ptr",0, "ptr*",pdoc)
return ComObj(9,ComObjQuery(pdoc,IID,IID),1), ObjRelease(pdoc)
}
}
}



[/code]
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Cycle through HTML elements

18 Jul 2018, 11:06

You could also try using this function found here https://sites.google.com/site/ahkref/cu ... thtmlbytag
I have used it with great success.
rynsp8
Posts: 46
Joined: 30 Jan 2018, 12:45

Re: Cycle through HTML elements

20 Jul 2018, 08:12

It seems like it could be useful, but it's returning an error: "Call to nonexistent function"
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Cycle through HTML elements

20 Jul 2018, 12:39

So you need to make sure that you include the Function somewhere in your script

Code: Select all

GetHTMLbyTag(HTMLSource, Tag, Occurrence=1, Format=0) {
    ;Format 0:Text 1:HTML 2:DOM
    ComError := ComObjError(false), `(oHTML := ComObjCreate("HtmlFile")).write(HTMLSource)    
    if (Format = 2) {
        if (innerHTML := oHTML.getElementsByTagName(Tag)[Occurrence-1]["innerHTML"]) {
            `(oDOM := ComObjCreate("HtmlFile")).write(innerHTML)
            Return oDOM, ComObjError(ComError)
        } else
            Return "", ComObjError(ComError)
    }
    return (result := oHTML.getElementsByTagName(Tag)[Occurrence-1][(Format ? "innerHTML" : "innerText")]) ? result : "", ComObjError(ComError)
}
This function can be put anywhere in your script (most people put them at the bottom or a specific section of their script), then you can call that function as many times as you want.
rynsp8
Posts: 46
Joined: 30 Jan 2018, 12:45

Re: Cycle through HTML elements

24 Jul 2018, 08:48

Can I ask a separate question here MannyKSoSo?

The code you've referenced above, is that strictly AHK? Or is it something else?

I've been able to use JS in the IE console to iterate through and return the innerHTML I'm looking for, but that's using JS in the IE console. Now I need to translate it to work with AHK. I have it below for reference:

Code: Select all

var pre = document.parentWindow.frames("FrameB").document.all.tags("pre")

for(x = 0; x <= pre.length; x++){
    if(pre[x].innerHTML.indexOf("CLMPRUN") >= 0){
        pre[x].innerHTML
    }
}

Now, the above code returns exactly what I'm looking for. Now the trick is to have it useful in AHK.
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Cycle through HTML elements

24 Jul 2018, 08:55

The code I provided is by A_Samurai that is made for AHK. You can translate it to other languages, but it was made for AHK.
So with the function I gave just put it in your script, then call it similar to so.

Code: Select all

MsgBox % GetHTMLbyTag(Var, "pre",, 1)
This will get you the first occurrence of anything with the Tag "pre" but the Var is the variable you need to load that has the html code in it. You can download the html that way or for testing just copy it into clipboard and changed Var to clipboard and it will give you a message box of the first pre tag till where that tag ends.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Rohwedder and 345 guests