0

2+ years ago I asked basically this same question (Automate IE via Excel to fill in a dropdown and continue) and it worked perfectly until a couple of months ago. The code that I previously used is below:

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

My Web Scraping code is below:

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 

To this point everything works fine and the 4th option on the drop-down is displayed on the page. What's not working now is the following line of code:

Call TriggerEvent(htmlDoc, nodeDeviceTypeDropdown, "change")

I have tried just about everything imaginable in place of that "change" but nothing seems to work to indicate within IE that I've made my selection which would normally display the next drop-down that I need to work with?? The HTML code for the drop-down object is below:

<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>

I'm not sure exactly what changed from before that stopped this from working. Any ideas on how to make this work would be appreciated. Thanks.

Original HTML Code that worked with my current vba shown below:

Original HTML Code below

John Wilson
  • 100
  • 1
  • 7

1 Answers1

1

I see that the macro still works. I am surprised because IE is actively being phased out by MS and the site works with many dynamic elements.

Of course I looked at your problem and was also pleased to see that you have adapted the code to the new design of the page. Now it seems that something has changed again. But it's nothing bad. It's a time problem.

As with other parts of the code, a pause needs to be inserted to give the page time to build up the selection part. For me, it works when the pause is inserted at the following point:

'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
Zwenn
  • 2,147
  • 2
  • 8
  • 14
  • The delay doesn't work for me. I run my code up to the 'Set htmlDoc = ie.document' and exit In the Immediate Window: 'htmlDoc.getElementsByClassName("card border-tiles shadow h-100 padY-2 device-off")(0).Click' 'Set nodeDeviceTypeDropdown = htmlDoc.getElementById("DeviceOptions")' 'nodeDeviceTypeDropdown.selectedIndex = 4' All is good to here and the dropdown displays "Crane Device" 'Call TriggerEvent(htmlDoc, nodeDeviceTypeDropdown, "change")' generates an error and doesn't display the next dropdown. I've tried just about every variation of "change" but no luck. – John Wilson Apr 29 '23 at 15:23
  • I was hoping that you would reply to my question. Your answer to my original question 2 years ago was perfect. A simpler explanation of my new problem: 'nodeDeviceTypeDropdown.selectedIndex = 4' displays the dropdown selection with the original code after 'Call TriggerEvent(htmlDoc, nodeDeviceTypeDropdown, "change") the dropdown would shrink and the next dropdown would appear That "change" event no longer works and generates an error. The 2nd dropdown never appears. – John Wilson Apr 29 '23 at 17:50
  • Gonna' have to kick myself in the a$$ for this one but as often as I looked at this and retried it multiple times, I didn't put the pause in the exact place that you specified. Just looked at it again today, put the pause where you suggested and it worked perfectly. Why it worked for over a year without the pause and now needs it, I have no idea but it really doesn't matter at this point. Thank you for looking at this (again) and thank you for solving it for me (again). – John Wilson May 23 '23 at 15:47
  • @JohnWilson Oh, dear, sorry. Actually, I wanted to edit the whole macro into my answer again. But I couldn't do it right away and didn't think of it anymore. Good that you have now checked it again yourself and found the error. I think the layout of the page was changed, wasn't it? When the macro was first created, the controls were still enabled individually when the values were entered in the previous control. – Zwenn May 24 '23 at 14:29