Search code examples
javascripthtmldrop-down-menuautohotkeyselectedindex

AutoHotKey Com Object get select list


I'm using the following code to just select a drop down menu on a webpage, which I would think would be pretty simple. Unfortunately, I get an "Unknown Name" error whenever I try to change the selectedIndex of the select list.

This results in an empty message:

test := ie.document.getElementByID("mySelect").selectedIndex.Value
MsgBox %test%

Here is the the HTML:

<SELECT onchange=onchangeSelectOption(); onfocus="hint('Select option');" id=mySelect name=mySelect> 
    <OPTION value=option_1>Option 1</OPTION>
</SELECT>

I've tried this to change the selectedIndex and manually trigger the onchange event:

ie.document.getElementByID("mySelect").selectedIndex := 1
ie.document.getElementByID("mySelect").fireEvent("onChange")

I've also tried to get the value in other ways, like:

test := ie.document.all.mySelect.selectedIndex
MsgBox %test%

as well as:

ie.document.all.mySelect.Click()

One thing that may be significant. You can't right-click the select list and view source, but you can do it nearby and find the select list in the DOM. Also, I believe the options for the select list are loaded dynamically via javascript, so I thought maybe the select wasn't available when I was trying to access it. I incorporated a long wait time to ensure that everything was loaded, but it still doesn't recognize the name or ID of the select list.

Any ideas?

EDIT: The HTML is inside a frame.


Solution

  • For anyone who may be looking for an answer in the future, I realized the page was using frames, and learned from this post that you have to first target the frame, and then you can manipulate the document within the frame. I was getting an empty MsgBox because I was attempting to find the element with the ID mySelect in a document where it didn't exist.

    Frame := ComObj(9,ComObjQuery(ie.document.getElementById("_Menu_target").contentWindow,"{332C4427-26CB-11D0-B483-00C04FD90119}","{332C4427-26CB-11D0-B483-00C04FD90119}"),1)
    msgbox % Frame.document.documentElement.innertext