1

I am relatively new to IE Automation, javascripts and frames. I also am not sure if I have the same issue as VBA - Handle Javascript pop up with IE automation. Pls read on.

Pre-Requisites: IE11, VBA for Excel (2010)

So I am trying to access the details of a webpage dialog box, wherein you are to: 1. Click a button (to validate address given a zip code) 2. A custom webdialog box containing a frame opens asking you to select from a list of cities in which that particular zip code is applicable. The list of cities isn't a combobox but anchor elements displayed in a table. 3. So I click the particular city using element.click but nothing happens.

Clicking that same anchor element with a manual mouse click triggers the javascript event onclick, which by the way calls another function.

HTML:

<a class="thisClass" onclick="return thisFunction("1", "arg2", "targetFrame");" href="thisURL#">

And the JavaScript:

function thisFunction( arg1, arg2, targetFrame)  {   
    var thisVar1 = new Object();      
    thisVar1.argument1= arg1;   
    thisVar1.argument2= arg2;   
    window.returnValue = thisVar1 ;      
    window.close();    
}

And my code:

Set frameEl = IE.Document.getElementById("frameID") 'this is accessing the frame of the webpage dialog popup
Set elem = frameEl.contentDocument
Set anchorElemCollection = elem.getElementsByTagName("a")

For Each anchorElem In anchorElemCollection
    If anchorElem.className Like "thisClass" Then
         If anchorElem.innertext Like aParticularValue Then
               anchorElem.Click
               Exit For
         End If
    End If
Next

The anchorElem.Click part isn't working. The webdialog popup window doesn't select the anchorElem, and it doesn't close. HELP! What did I do wrong?

Community
  • 1
  • 1
Jp Serame
  • 101
  • 1
  • 8
  • Are you actually looking in the right place for the right element? Is the `.Click` not working or is the correct `anchorElem` never found and no click is ever performed? Step through your code with F8 or add a debug.print above `anchorElem.Click` that shows the element was actually found. –  May 09 '15 at 19:16
  • I used a breakpoint in the debugger (F12) and yes it did step and performed a click. I just dont understand why it didnt actually clicked the anchor. – Jp Serame May 09 '15 at 23:57

1 Answers1

1

If you can determine the values that would be passed into arg1 and arg2, try running the javascript routine directly.

ie.document.parentWindow.execScript "javascript:thisFunction(some_value1, some_value2, frameEl)
  • I'm gonna try this now. getting back on you later on the results. thanks! – Jp Serame May 09 '15 at 23:58
  • Hi @Jeeped. I have tried the execScript but I have the same problem as before, nothing happens! Yes I am sure of the params passed because I derived the function from the outerHtml of the anchor tag. Also, I cannot put a breakpoint on the exact line to which the code runs on the webdialog popup. Is this normal? Cannot trace it efficiently, step-by-step. – Jp Serame May 10 '15 at 14:14
  • 1
    Hi again. I have read at http://stackoverflow.com/questions/4511874/cannot-programmatically-trigger-jquery-click-event that programmatically clicking a link that contains an href is not possible. If any, can there be workarounds with these? – Jp Serame May 11 '15 at 07:04
  • I've had limited success clicking links that execute script. That is why I prefer the `.execScript` method. –  May 11 '15 at 07:17
  • Yeah I also tried doing it that way (execScript) but still to no avail - debugger tells that it clicked the link but nothing happens anyways. Perhaps link.click does not behave the same way as button.click. – Jp Serame May 11 '15 at 14:42