【问题标题】:Pressing enter on IE with Excel VBA “Press Enter to Search”使用 Excel VBA 在 IE 上按 Enter 键“按 Enter 键搜索”
【发布时间】:2017-08-03 00:54:39
【问题描述】:

我正在处理一个 Web 表单文本字段,当按下“Enter”时,它将启动对其内容的搜索。

我知道如何启动所有其他事件侦听器,但我无法触发按下“Enter”事件。它没有与其他事件一起列出。即 onchange、onclick、onblur

我在 Excel VBA 中使用 CreateObject("Shell.Application") 作为父对象来控制现有的 IE 浏览器。

我尝试了 sendkeys,但对 VBA 关注的内容有疑问。它在我的 IDE 或电子表格本身中输入。

这不是一个公共网站。它是一个带有事件(blur、change、focus、keydown、mousedown、mousemove)的输入标签。

  With Tex_Box
       .focus
       .keydown
       .innertext = Field_Text
       .change
       .focus
       .blur
   End With

【问题讨论】:

  • 这是一个有趣的问题,但更多代码(最好足以使其成为minimal reproducible example)会有所帮助。
  • 对不起,我不知道有任何其他网站是公开的,举个例子。
  • 现在看。似乎“按 Enter 搜索”是一种无按钮的发起事件的方法。
  • 这里有一个链接可以帮助stackoverflow.com/questions/11655591/…

标签: excel vba web-scraping


【解决方案1】:

所以我发现 stackoverflow.com/questions/11655591/... 描述了如何在使用 Sendkeys 之前为 Internet Explorer 提供焦点。然而,“如何”只是难题的一部分,另一部分是在程序调用发送键“~”(Enter)之前找到需要关注的“什么”。由于未知数量的发送键“{TAB}”将光标放在所需字段上,我创建了一个循环,该循环将不断迭代所有文本字段,直到出现部分字符串“焦点”在所需文本框的名为 classname 的属性中。一旦发生这种情况,程序就会有一个标记让它知道调用发送键“~”。

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long
Dim HWNDSrc As Long

text_again:
With DOCK_Tex_Box
    .focus
    .keydown
    .innertext = Field_Text
    .change
    Sleep (500)
    .focus
    .blur
    HWNDSrc = IE.HWND 
   For i = 1 To 100
      DoEvents
      SetForegroundWindow HWNDSrc
      DoEvents
      Application.SendKeys ("{TAB}"), True
      DoEvents
      Sleep 1000
      If InStr(.classname, "prompt") > 0 Then 'If the for loop goes to quickly the field loses focus and then the text clears out.
          DoEvents
          Sleep 500
          GoTo text_again:
      End If

      If InStr(.classname, "focus") > 0 Then 'Text field has focus
        Sleep 1000
        Exit For
      End If
   Next
End With
Application.SendKeys ("~"), True
DoEvents
Sleep 500

【讨论】:

    猜你喜欢
    • 2011-11-05
    • 2022-01-17
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多