title: SendMessage {ENTER} to Internet Explorer browser iFrame element
body: <p>I have a little project that needs me to automate IE.
  I've used <code>SHDocVw.InternetExplorer</code> class for the initial move. (attaching to IE)</p>
  
  <p>But after that I want to use the Windows API to get focus to browser element and to click it. </p>
  
  <p>I know that I can do this job more easy with the built in function of <code>SHDocVw.InternetExplorer</code>, or use 3rd party class like <strong>Watin</strong></p>
  
  <p>But this time I want to use Windows API.</p>
  
  <p>This is work perfect when I open IE browser and find some links/element to click</p>
  
  <p>I used the key press {TAB} to iterate thru HTML elements then read the IE status bar to get its link. Then I use the key press <code>{ENTER}</code> to click/execute it.</p>
  
  <p>I got problem when the element that are selected by {TAB} are inside iFrame, the keypress {ENTER} not work.</p>
  
  <p>Here is my sample code</p>
  
  <pre class="lang-vb prettyprint-override"><code>Imports WindowsApplication1.win_Api
  Module browser
      Public Class IE
          Private Shared IE As SHDocVw.InternetExplorer
          Sub New(Optional ByVal url As String = "about:blank")
              Dim bw As New SHDocVw.InternetExplorer
              IE = bw
              Try
                  IE.Navigate(url)
              Catch ex As Exception
              End Try
          End Sub
          Shared Sub click(byval url as string)
              Dim Z As Integer = 0
              Do While Z = 0
                  IE.Stop()
                  'Send keboard press TAB this API is work perfect
                  SendMessage(IE.HWND, WM_IME_KEYDOWN, Keys.Tab, 0)
                  IE.Stop()
                  If IE.StatusText = url Then
                      Z = 1
                  End If
              Loop
              For Each I As IntPtr In GetChildWindows(IE.HWND)
                  Dim L As Integer = 256
                  Dim sb As New System.Text.StringBuilder(L)
                  GetClassName(I, sb, L)
                  If sb.ToString = "Internet Explorer_Server" Then
                      'Send keyboard press ENTER, this work
                      'But not work if the element selected by TAB action is on iframe
                      SendMessage(I, WM_CHAR, Keys.Enter, 0)
                  End If
              Next
          End Sub
      End Class
  End Module
  </code></pre>