Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

html - How to select an specific item on a drop down list on ASPX site

I want it to be as if the user had clicked on "Dental." However, when I run the following code, nothing happens. I want it to be as if the user had clicked on "Dental." However, when I run the following code, nothing happens. I want it to be as if the user had clicked on "Dental." However, when I run the following code, nothing happens.

'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

    'Set IE = Get_IE_Window(URL)
    'If IE Is Nothing Then
     '   Set IE = New SHDocVw.InternetExplorer
    'End If

   ' With IE
    '    SetForegroundWindow .hwnd
     '   .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 post.Click: Exit For
    'Next post

'End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...