Sunday, August 21, 2011

Click on Element within a Frame using AHK

The syntax is tricky and threw me off this project for months. I finally have cleared it up, and here it is for posterity:

The syntax for plain javascript, executable from internet explorer DebugBar v5.4.1 is:
document.parentWindow.actualframename.document.getElementByID("actualID").click()

In distinction, the syntax for AHK COM SCRIPTS is:
COM_Invoke(pwb, "document.parentWindow.actualframename.document.getElementByID[actualID].click")

NOTICE (I usually don't shout, but in this case I will make an exception): THE ARGUMENT OF getElementById IN JAVASCRIPT IS ENCLOSED IN PARENTHESES AND QUOTES. THE SAME ARGUMENT IN AHK IS ENCLOSED INSTEAD IN BRACKETS AND NO QUOTES. ALSO THE METHOD IN JAVASCRIPT HAS PARENTHESES AFTER IT click() WHEREAS IN AHK IT DOES NOT click (THE REASON FOR THESE DIFFERENCES IS THE AHK COM_Invoke FUNCTION, WHICH REQUIRES ARGUMENTS CONTAINED IN PARENTHESES AND QUOTES.)

Also it is worth saying what is the quickest way to find the actual frame name (yes it is the name, not the ID) and the actual element ID (yes ID, not the name). Answer: A magnificent AHK script called iWebBrowser2 Learner Build 2.5, by "Tank, Jethrow, and Sinkfaze" of the AHK forum. Just hover over the element until it has a red box around it, and read the answer in the dialog box.

More info can be found at these forum posts: (primer on COM functions, and link to the all-important GetWebBrowser() function the COM functions depend on)
http://www.autohotkey.com/forum/topic51020.html
http://www.autohotkey.com/forum/topic24930.html

Thank you Sean, Tank, Jethrow, and Sinkfaze, whoever you are.