This particular web page isn’t using <select>
and <option>
. That suggests to me that they are using some custom JavaScript to simulate a drop-down list using the illustrated <div>
and <span>
elements. In addition, they are using onselect
rather than onclick
to trigger event handlers.
I can’t replicate your test case. However, I did make a test case of my own, and I think it works. In short, replace post.Click
with post.onselect
. The problem with your code was that it was trying to trigger an onclick
handler that didn’t exist on the target <span>
!
HTML
<html>
<head>
<script type="text/javascript">
function clk() { alert('Dental clicked'); } // *** So I could see if Click had an effect
function sel() { alert('Dental selected'); } // *** Simulate the given testcase
</script>
</head>
<body>
<div class="cboGroupGrid">
<span class="cboItem" option="0"></span>
<span class="cboItem" option="1" onselect="sel();" onclick="clk();">Dental</span>
<span class="cboItem" option="2">Health</span>
<span class="cboItem" option="3">Unknown Product</span>
</div>
</body></html>
VBA
In the code below, '''
are things I commented out because I didn’t have them and they weren’t essential to the task as I understand it.
Public Sub IE_Search_and_Extract()
Dim URL As String
Dim IE As SHDocVw.InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim response As String
'''response = MsgBox("Login ", vbYesNo + vbQuestion, "login")
'''If response = vbYes Then
''' URL = " "
'''Else
'''End If
URL = "prashant.htm" ' ** Local testcase
'''Set IE = Get_IE_Window(URL) ' ** I don't have the code for this function
'''If IE Is Nothing Then
Set IE = New SHDocVw.InternetExplorer
'''End If
With IE
'''SetForegroundWindow .Hwnd ' ** I don't have this function
.navigate URL
.Visible = True
While .Busy Or .readyState <> READYSTATE_COMPLETE
DoEvents
Wend
'.document.getElementById("btnLogin").Click
' While .Busy Or .readyState <> READYSTATE_COMPLETE
' DoEvents
' Wend
'Application.Wait (Now + TimeValue("0:00:5"))
Set HTMLdoc = .document
End With
Dim post As Object, elem As Object
For Each post In HTMLdoc.getElementsByClassName("cboItem")
If InStr(post.innerText, "Dental") > 0 Then ' ** Use a block If, not inline.
'post.Click ' ** This does work, and triggers clk()
post.onselect ' ** This triggers sel()
Exit For
End If
Next post
End Sub
Style suggestion: If you have more than one thing in a Then
or Else
block, always use the multiline form. That way there’s no ambiguity about whether the :
ends the Then
block or not.
5
solved How to select an specific item on a drop down list on ASPX site