WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE with MSIE
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

IE Click or Focus Methods Fail

 Keywords: IE MSIE Internet Explorer InternetExplorer.Application IE 7 8 9 Click Focus Method Fail Button Compatibility View Web Page HTM HTML CreateEvent InitEvent Dispatch Event HTMLEvents Issue Problem Ignored Enable Protected Mode

Question:

I am encountering a problem where "click" doesn't seem to work when automating an IE 9 browser. The same code works if I use IE7.

Answer:

I currently suspect the webbpage design problem for IE9. The strange thing about this webpage is that you cannot set focus on ANY of the HTML elements.

I see you are trying to click on an a href link. One workaround is to get the href value of the link and use the navigate method to navigate to that page.

I see when I load the webpage that the Compatibility icon gets displayed. This indicates compatibility issue with this webpage on IE9. Try adding the webbage to compatibility view. http://blogs.msdn.com/b/ie/archive/2009/06/17/compatibility-view-and-smart-defaults.aspx

User Reply:

Thank you. adding the webpage to Compatibility View resolves the focus and click issue.

More:

Here is a complete code sample with a webpage which does not allow the click method without running in compatibility mode.
;IE9 Click Fix
#DefineFunction WaitForBrowser(Browser)
   While Browser.Busy || Browser.ReadyState <> 4
     TimeDelay(0.5)
   EndWhile
   Return
#EndFunction

url = "http://sirsi.emu.edu/uhtbin/webcat"

objBrowser = ObjectOpen("InternetExplorer.Application")
objBrowser.Visible = @TRUE

objBrowser.Navigate(url,0,'','','')
WaitForBrowser(objBrowser)

objAnchorElements = objBrowser.document.GetElementsByTagName("a")
ForEach objElement In objAnchorElements

   If objElement.innerText == "Power Search" || objElement.innerText == "Advanced Search" Then

      ; CLICK FAILS unless page is set to view in Compatibility View.
      ; objElement.Click

      ; WORKAROUND 1
      ; Get href then navigate
      ;objBrowser.Navigate(objElement.href)

      ; WORKAROUND 2
      ; createEvent initEvent DispatchEvent
      objEvent = objBrowser.document.createEvent("HTMLEvents")
      objEvent.initEvent("click", @TRUE, @TRUE)
      objElement.dispatchEvent(objEvent)
      Break
   EndIf
Next

WaitForBrowser(objBrowser)
Message('','Advanced Search page should now be visible')
objBrowser.quit
objBrowser = 0

Article ID:   W18130
Filename:   IE9 Click or Focus Methods Fail.txt
File Created: 2013:02:21:08:51:18
Last Updated: 2013:02:21:08:51:18