Search code examples
excelvbainternet-explorermshtml

InternetExplorer - delete specific element from IE.Document


I am using VBA to automate the pulling of reports from a structured web page.

I have the following code block which changes the dropdown element for each element in an array"

With IE
For Each itemName In itemNames()
i = i + 1
Module2.check_and_wait_for_item ("cboDistrict")
Set Dropdown = .document.getElementsByName("cboDistrict")(0)
Dropdown.Value = itemValues(i)
Dropdown.FireEvent ("onchange")



check_and_wait_for_item ("cmdRptPreview")
Set btnPreview = .document.getElementsByName("cmdRptPreview")(0)
btnPreview.Click

check_and_wait_for_item ("BtnExport")
Set HTMLDoc = .document
write_file_to_dir HTMLDoc, "C:\Users\user\Documents\____thing\", itemName & ".html"
.GoBack

Next itemName
End With

The problem arises with IE.GoBack. I have a function that checks if JavaScript has finished executing by looping while a particular element is not present on the page. "Going back" puts the element back on the page even though the JavaScript event has not finished running.

This is why I want to delete the old "cmdRptPreview" element before I have my function check if JavaScript event has finished running.

Is there a way to do so? Appreciate the help.


Solution

  • The solution was to remove the Node containing the element being checked by JavaScript waiter function. After .GoBack:

    check_and_wait_for_item ("cmdRptPreview")
    .document.getElementsByName("cmdRptPreview")(0).ParentNode.RemoveChild .document.getElementsByName("cmdRptPreview")(0)