IE 9不接受SendKeys [英] IE 9 not accepting SendKeys

查看:197
本文介绍了IE 9不接受SendKeys的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发布在 IE 9不接受SendKeys下载文件,但这个问题与我收到的答案是分开的,以证明另一个问题。我的问题是,我不能让IE 9接受任何 SendKeys 。我尝试 Page Down 选项卡,所有的 F#键,都没有工作。

I posted on IE 9 not accepting SendKeys to download a file, but this problem is separate enough from the answer I received to justify another question. My problem is that I can't get IE 9 to accept any of the SendKeys. I have attempted Page Down, Tab, all of the F# keys, and none of them work.

这是我使用的代码:

Dim ie As Object

'This creates the IE object
Sub initializeIE()
   'call this subprocedure to start internet explorer up
   Set ie = CreateObject("internetexplorer.application")
   pos = 1
End Sub

'Initialize the class object
Private Sub Class_Initialize()
   initializeIE
End Sub

Function followLinkByText(thetext As String) As Boolean
  'clicks the first link that has the specified text
  Dim alink As Variant

  'Loops through every anchor in html document until specified text is found
  ' then clicks the link
  For Each alink In ie.document.Links
     If alink.innerHTML = thetext Then
          alink.Click
          'waitForLoad
          Application.Wait Now + TimeValue("00:00:01")
          Application.SendKeys "{PGDN}", True
          Application.SendKeys "{PGUP}", True
          'I've also tried calling it without Application before it
          SendKeys "{F1}", True
          SendKeys "{F2}", True
          'Etc... Each of these not being received by IE 9

          followLinkByText = True
          Exit Function
      End If
  Next

End Function

我有一个完全的损失,因为似乎大多数论坛或教程对IE不做任何不同9. IE对象是在类模块中创建的,并在 Class_Initialize 子文件中初始化。我不知道这是否有帮助,但我真的不知道为什么这不工作,任何帮助如何发送键到IE将不胜感激。

I'm at a total loss because it seems like most forums or tutorials don't do anything different for IE 9. The IE object is created in a class module and initialized in the Class_Initialize sub. I am not sure if that helps any, but I really have no idea why this isn't working and any help on how to send keys to IE would be greatly appreciated.

推荐答案

这实际上是我的回答这个问题,但可能仍然适用。

This is actually a copy of my answer to this question, but it may still apply.

当您尝试IE浏览器窗口是活动的您的 SendKeys ?如果没有,这将解释它不起作用。

Is the IE window active when you try your SendKeys? If not, this would explain it not working.

要激活您的窗口:

模块,放这行代码:

Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long

这将允许您访问 SetForegroundWindow 函数内置进入Windows。

This will allow you to access the SetForegroundWindow function built into Windows.

在您的代码中,与IE对象进行交互时,记录该窗口的HWND,如下所示:

In your code, while interacting with your IE object, record the HWND for that window like so:

Dim HWNDSrc As Long
HWNDSrc = ie.HWND

然后在您加载页面后,使用此继续,然后发送您的主要操作:

Then after you've loaded the page, use this to continue, then send your key actions:

SetForegroundWindow HWNDSrc

但是,这可能不是必需的,具体取决于您如何与IE进行交互。换句话说,如果您不需要看到/触摸窗口(您为 SendKeys ),您仍然可以使用代码中的对象进行交互。

However, this may not be necessary, depending on how you are interacting with IE. In other words, if you don't need to see/touch the window (you do for SendKeys), you can still interact using the object in code.

点击后,我看到您使用Application.Wait,但这并不能保证IE页面已经加载。这个功能应该有帮助。

Now, I see you using Application.Wait after you click, but that does not guarantee the IE page has loaded. This function should help with that.

 Public Sub WaitForIE(myIEwindow As InternetExplorer, HWND As Long, WaitTime As Integer)

    ' Add pauses/waits so that window action can actually
    ' begin AND finish before trying to read from myIEWindow.

    ' myIEWindow is the IE object currently in use
    ' HWND is the HWND for myIEWindow
    ' The above two variables are both used for redundancy/failsafe purposes.
    ' WaitTime is the amount of time (in seconds) to wait at each step below. 
    ' This is variablized because some pages are known to take longer than 
    ' others to load, and some pages with frames may be partially loaded,
    ' which can incorrectly return an READYSTATE_COMPLETE status, etc.

    Dim OpenIETitle As SHDocVw.InternetExplorer

    Application.Wait DateAdd("s", WaitTime, Now())

    Do Until myIEwindow.ReadyState = READYSTATE_COMPLETE
        ' Wait until IE is done loading page and/or user actions are done.
    Loop

    Application.Wait DateAdd("s", WaitTime, Now())

    While myIEwindow.Busy
        DoEvents  ' Wait until IE is done loading page and/or user actions are done.
    Wend

    On Error Resume Next
    ' Make sure our window still exists and was not closed for some reason...
    For Each OpenIETitle In objShellWindows
        If OpenIETitle.HWND = HWND Then
            If Err.Number = 0 Then
                Set myIEwindow = OpenIETitle
                Exit For
            Else
                Err.Clear
            End If
        End If
    Next OpenIETitle
    On Error GoTo 0

End Sub






如果您有长时间的风险,我已经使用这些建议更新了您的代码...


At the risk of being long-winded, I've updated your code with these suggestions...

' Added by Gaffi
Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long
Dim HWNDSrc As Long

Dim ie As Object


'This creates the IE object
Sub initializeIE()
   'call this subprocedure to start internet explorer up
   Set ie = CreateObject("internetexplorer.application")

' Added by Gaffi
   HWNDSrc = ie.HWND

   pos = 1
End Sub

'Initialize the class object
Private Sub Class_Initialize()
   initializeIE
End Sub

Function followLinkByText(thetext As String) As Boolean
  'clicks the first link that has the specified text
  Dim alink As Variant

  'Loops through every anchor in html document until specified text is found
  ' then clicks the link
  For Each alink In ie.document.Links
     If alink.innerHTML = thetext Then
          alink.Click
          'waitForLoad

' Added by Gaffi
          WaitForIE ie, HWNDSrc, 1
          SetForegroundWindow HWNDSrc

          'Application.Wait Now + TimeValue("00:00:01")
          Application.SendKeys "{PGDN}", True
          Application.SendKeys "{PGUP}", True
          'I've also tried calling it without Application before it
          SendKeys "{F1}", True
          SendKeys "{F2}", True
          'Etc... Each of these not being received by IE 9

          followLinkByText = True
          Exit Function
      End If
  Next

End Function

这篇关于IE 9不接受SendKeys的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆