【问题标题】:IE Handling in Excel macrosExcel 宏中的 IE 处理
【发布时间】:2019-05-29 16:31:29
【问题描述】:

我是 Excel 宏的新手,我正在尝试自动填充文本框并从下拉列表中选择选项,然后单击提交。

使用宏导航到一个页面,从该页面,我需要在文本框中输入文本,并根据建议,我需要选择选项。

选择该选项后,我需要点击继续使用宏。

这是当前代码(我尝试使用注释行但它不起作用)。

Sub Create_Change()
    Dim i As Long
    Dim URL As String
    Dim IE As Object
    Dim objElement As Object
    Dim objCollection As Object
    Dim HWNDSrc As Long
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    URL = "URL to Navigate"
    IE.Navigate URL
    Application.StatusBar = URL & " is loading. Please wait..."
    Do While IE.ReadyState = 4: DoEvents: Loop
    Do Until IE.ReadyState = 4: DoEvents: Loop
    Application.StatusBar = URL & " Loaded"
    ' IE.Document.All("Search for a template").Value = "text to search"
    Set IE = Nothing
    Set objElement = Nothing
    Set objCollection = Nothing
End Sub

HTML 代码:

<input title="Search for a template" 
       class="change-template__search-input ng-pristine ng-untouched ng-valid" 
       role="combobox" 
       aria-expanded="false" 
       aria-owns="typeahead-258-8737" 
       aria-autocomplete="list" 
       type="text" 
       placeholder="Search for a template" 
       ng-enter="getRecommendedTemplates(template.search)" 
       ng-model="template.search" 
       typeahead="template as template for template in getTemplateList($viewValue)" 
       typeahead-focus-first="false" 
       typeahead-on-select="getRecommendedTemplates($item)" 
       typeahead-min-length="3">

【问题讨论】:

  • 有可以测试的网址吗?

标签: excel vba


【解决方案1】:

我认为您需要搜索 Document.All() 返回的所有元素。尝试遍历返回的集合。如果控件有一个 id,这会容易得多,但是您必须检查每个具有 title = "Search for a template" 的元素。找到项目后,您可以设置文本。

我建议进行调试,这样您就可以看到 .All 返回的内容,然后您就可以从那里开始工作了。

【讨论】:

  • 感谢您的回复 drHodge。我尝试按照 Mariano 所述添加以下行,但文本未填充 IE.Document.all() 中的每个项目 If Item.getAttribute("title" ) = "搜索模板" Then Item.Value = "ORADB" End If Next Item 我错过了什么吗?
【解决方案2】:

欢迎来到 SO。如果您需要获取一个元素,正如霍奇博士所说,使用迭代器和某种 ID 属性通常就足够了。我在一分钟前用谷歌试过这个:

Dim Item As Object
For Each Item In IE.Document.all()
If Item.getAttribute("title") = "Buscar" Then
Item.Value = "Test"
End If
Next Item

我也让它按空格,向下箭头并输入,以便对建议和搜索部分做一些事情:

SendKeys ("{ }")
Application.Wait Now + TimeSerial(0, 0, 1)
SendKeys ("{DOWN}"), True
Application.Wait Now + TimeSerial(0, 0, 1)
SendKeys ("{ENTER}"), True

(我不建议在任何脚本上使用 SendKeys,虽然;它非常不可靠。)

【讨论】:

  • 感谢您的回复 Mariano。我尝试添加下面提到的行,但文本未填充 IE.Document.all() 中的每个项目如果 Item.getAttribute("title") = "搜索模板" Then Item.Value = "ORADB" End If Next Item 我遗漏了什么吗?
  • 嗨菲尔宾。该 HTML 是在实时导航中下载的,还是您计算机上的文件?你确定确实有一个具有该标题的元素吗?我刚刚使用您的确切 HTML sn-p (另存为文件)制作了相同的循环并使其正常工作;这就是为什么我猜测您正在搜索的 HTML 发生了一些变化。这是整个 Sub:codeshare.io/5zkL8r 请注意,这不是生产代码:它可能会以多种方式失败,而且效率不高.
【解决方案3】:

您可以尝试以下方法:

1) 关注和改变

With ie.document.querySelector("[title='Search for a template']")
    .focus
    .value = "abcd"
End With

2) Javascript 设定值

ie.document.parentWindow.execScript "document.querySelector('[title=\'Search for a template\']').value = 'abcd';"

3) 附加事件并触发

Dim evt As Object
Set evt = ie.document.createEvent("HTMLEvents")
evt.initEvent "change", True, False

With ie.document.querySelector("[title='Search for a template']")
    .Focus
    .Value = "abcd"
    .dispatchEvent evt
End With

4) 火灾事件

With ie.document.querySelector("[title='Search for a template']")
    .Focus
    .Value = "abcd"
    .FireEvent "onchange"
End With

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-30
    • 2016-12-02
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多