如何在ASPX网站

时间:2017-12-20 16:15:22

标签: html vba excel-vba dom web-scraping

我希望它好像是用户点击了"牙科。"但是,当我运行以下代码时,没有任何反应。 我希望它好像是用户点击了"牙科。"但是,当我运行以下代码时,没有任何反应。 我希望它好像是用户点击了"牙科。"但是,当我运行以下代码时,没有任何反应。

'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

1 个答案:

答案 0 :(得分:0)

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

我无法复制您的测试用例。但是,我确实制作了自己的测试用例,我认为它有效。简而言之,将post.Click替换为post.onselect。您的代码存在的问题是,它试图触发目标onclick上不存在的<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

在下面的代码中,'''是我注释掉的内容,因为我没有这些内容,而且根据我的理解,它们对于任务来说并不重要。

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

样式建议:如果ThenElse块中有多个内容,请始终使用多行表单。这样,:是否结束Then阻止是不明确的。