通过 Excel 自动化 IE。下拉“更改”停止工作

问题描述 投票:0回答:1

2 多年前我基本上问过同样的问题(通过 Excel 自动化 IE 以填写下拉列表并继续)并且它在几个月前一直运行良好。我之前使用的代码如下:

Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)
Dim theEvent As Object
htmlElementWithEvent.Focus
Set theEvent = htmlDocument.createEvent("HTMLEvents")
theEvent.initEvent eventType, True, False
htmlElementWithEvent.dispatchEvent theEvent

我的网页抓取代码如下:

Private Sub SelectDropDown()
ForceIEClose
Set ie = CreateObject("InternetExplorer.Application")
With ie
    .navigate "https://a810-dobnow.nyc.gov/publish/Index.html#!/"
    .Visible = True   ' can be set to false to speed things up
End With
Do Until ie.readyState = 4: DoEvents: Loop
Set htmlDoc = ie.document
'htmlDoc.getElementsByClassName("white ng-scope")(3).Click           ' {Device Search} button
' New: "Search by device type"
htmlDoc.getElementsByClassName("card border-tiles shadow h-100 padY-2 device-off")(0).Click           ' {Device Search} button
On Error Resume Next
Set nodeDeviceTypeDropdown = htmlDoc.getElementById("DeviceOptions")    ' {Device Type} DropDown
Application.Wait (Now + TimeSerial(0, 0, 1))
On Error GoTo 0
If Not nodeDeviceTypeDropdown Is Nothing Then
    nodeDeviceTypeDropdown.selectedIndex = 4 

至此一切正常,下拉列表中的第 4 个选项显示在页面上。 现在不起作用的是以下代码行:

Call TriggerEvent(htmlDoc, nodeDeviceTypeDropdown, "change")

我已经尝试了几乎所有可以想象的东西来代替那个“改变”,但是在 IE 中似乎没有任何东西可以表明我已经做出了我的选择,通常会显示我需要使用的下一个下拉菜单? 下拉对象的 HTML 代码如下:

<select required="" class="form-control ng-pristine ng-empty ng-invalid ng-invalid-required ng-touched" id="DeviceOptions" ng-model="Criteria.DeviceOptions" ng-class="{'has-error': (IsDeviceSearchClick &amp;&amp; !Criteria.DeviceOptions)}">
                        <option class="selectPlaceholder" name="device" value="" hidden="" selected="selected">Select Device Type</option>
                        <option value="1">Boilers</option>
                        <option value="2">Elevator</option>
                        <option value="3">Crane Prototype</option>
                        <option value="4">Crane Device</option>
                    </select>

我不确定到底是什么改变了之前阻止它工作的能力。任何关于如何使这项工作的想法将不胜感激。谢谢。

与我当前的 vba 一起使用的原始 HTML 代码如下所示:

html excel vba web-scraping internet-explorer
1个回答
0
投票

我看到宏仍然有效。我很惊讶,因为 IE 正在被 MS 积极淘汰,并且该网站使用许多动态元素。

当然我看了你的问题,也很高兴看到你已经将代码适配页面的新设计。现在似乎又发生了一些变化。但这没什么不好的。这是一个时间问题。

与代码的其他部分一样,需要插入一个暂停,让页面有时间建立选择部分。对我来说,在以下位置插入暂停时它会起作用:

'Open the Device Search section
htmlDoc.getElementsByClassName("card border-tiles shadow h-100 padY-2 device-off")(0).Click
Application.Wait (Now + TimeSerial(0, 0, 1)) 'Possibly adjust the pause
© www.soinside.com 2019 - 2024. All rights reserved.