本文介绍了如何在ASPX网站的下拉列表中选择特定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望它就像用户单击牙科一样。但是,当我运行以下代码时,没有任何反应。
我希望它就像用户单击牙科一样。但是,当我运行以下代码时,没有任何反应。
我希望它就像用户单击牙科一样。但是,当我运行以下代码时,什么也没发生。

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


推荐答案

此特定网页未使用< select> 和< option> ; 。这向我暗示了他们正在使用一些自定义JavaScript来模拟下拉列表,其中使用了图示的< div> 和< span> 元素。此外,他们使用 onselect 而不是 onclick 来触发事件处理程序。

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.

我无法复制您的测试用例。但是,我确实做了一个自己的测试用例,并且我认为它是可行的。简而言之,将 post.Click 替换为 post.onselect 。您的代码的问题在于,它试图触发目标< span> 处理程序$ c>!

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



在以下代码中, '''是我注释掉的东西,因为我没有它们,而且按照我的理解,它们对于任务不是必需的。

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

风格建议:如果某件物品中有多个物品然后或 Else 块,请始终使用多行形式。这样,对于:是否结束 Then 块就不会产生歧义。

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.

这篇关于如何在ASPX网站的下拉列表中选择特定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:06