I am trying to click a drop down option from a HTML page with my VBA script, however I am struggling to work out how to do this.
Essentially my code opens the page, inputs some variables, then I want to select to export this as PDF from the list.
Dim oIE As Object
Dim appIE As InternetExplorerMedium
Dim XMLHTTP As Object
Set XMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")
Dim Disbuttons As Object
Set appIE = New InternetExplorerMedium
With appIE
.Visible = True
.navigate "WEBPAGE"
End With
Do
DoEvents
Loop Until appIE.readyState = 4
appIE.document.getElementById("ctl32_ctl04_ctl03_txtValue").Value = "03/10/2022"
appIE.document.getElementById("ctl32_ctl04_ctl05_txtValue").Value = "04/10/2022"
appIE.document.getElementById("ctl32_ctl04_ctl07_divDropDown_ctl00").Value = "21669"
appIE.document.getElementById("ctl32_ctl04_ctl00").Click
Application.Wait (Now TimeValue("0:00:10"))
appIE.document.getElementById("ctl32_ctl05_ctl04_ctl00_Menu").Click
appIE.document.querySelector("[title='PDF']").Click
The part that isn't working is;
appIE.document.getElementById("ctl32_ctl05_ctl04_ctl00_Menu").Click
appIE.document.querySelector("[title='PDF']").Click
The HTML for the drop down is:
<DIV id=ctl32_ctl05_ctl04_ctl00_Menu class=MenuBarBkGnd style="POSITION: absolute; PADDING-BOTTOM: 1px; PADDING-TOP: 1px; PADDING-LEFT: 1px; Z-INDEX: 1; DISPLAY: none; PADDING-RIGHT: 1px; VISIBILITY: hidden" _oldDisplayMode="block"><DIV class=DisabledButton><A onclick="$find('ctl32').exportReport('XML');" title="XML file with report data" class=ActiveLink style="TEXT-DECORATION: none; WHITE-SPACE: nowrap; PADDING-BOTTOM: 3px; PADDING-TOP: 3px; PADDING-LEFT: 32px; DISPLAY: block; PADDING-RIGHT: 8px" href="javascript:void(0)" alt="XML file with report data" _selected="false">XML file with report data</A> </DIV>
<DIV class=DisabledButton><A onclick="$find('ctl32').exportReport('CSV');" title="CSV (comma delimited)" class=ActiveLink style="TEXT-DECORATION: none; WHITE-SPACE: nowrap; PADDING-BOTTOM: 3px; PADDING-TOP: 3px; PADDING-LEFT: 32px; DISPLAY: block; PADDING-RIGHT: 8px" href="javascript:void(0)" alt="CSV (comma delimited)" _selected="false">CSV (comma delimited)</A> </DIV>
<DIV class=DisabledButton><A onclick="$find('ctl32').exportReport('PDF');" title=PDF class=ActiveLink style="TEXT-DECORATION: none; WHITE-SPACE: nowrap; PADDING-BOTTOM: 3px; PADDING-TOP: 3px; PADDING-LEFT: 32px; DISPLAY: block; PADDING-RIGHT: 8px" href="javascript:void(0)" alt="PDF" _selected="false">PDF</A> </DIV>
<DIV class=HoverButton><A onclick="$find('ctl32').exportReport('WORDOPENXML');" title=Word class=ActiveLink style="TEXT-DECORATION: none; WHITE-SPACE: nowrap; PADDING-BOTTOM: 3px; PADDING-TOP: 3px; PADDING-LEFT: 32px; DISPLAY: block; PADDING-RIGHT: 8px" href="javascript:void(0)" alt="Word" _selected="true">Word</A> </DIV>
<DIV style="HEIGHT: 149px; WIDTH: 26px; POSITION: absolute; LEFT: 0px; FILTER: alpha(opacity=5); Z-INDEX: -1; TOP: 0px; BACKGROUND-COLOR: black; opacity: 0.05"></DIV></DIV>
With specifically the exportReport('PDF') being the one of interest.
Any advice on this and where I'm going wrong would be greatly appreciated.
CodePudding user response:
title
is probably not the best attribute to search for (in context of the whole document), its not guaranteed to be unique, or that the first instance is the element you are looking for.
Dim Menu As Object
Dim Item As Object
Set Menu = appIE.document.getElementById("ctl32_ctl05_ctl04_ctl00_Menu")
If Not Menu Is Nothing Then
Set Item = Menu.querySelector("div[title=PDF]")
If Item Is Nothing Then
Debug.Print "couldnt find item"
Else
Item.Click
End If
Else
Debug.Print "couldnt find menu"
End If
Looking at the HTML, you can probably skip all of that by directly calling the export function. (in theory the following should work)
appIE.Document.parentWindow.execScript "$find('ctrl32').exportReport('PDF');"