VBA-IE10存储表对象?

时间:2017-03-03 02:42:03

标签: html vba internet-explorer

我已经想出了如何通过基于Web的表进行旋转以查找特定信息。在表的末尾是我想要与<td>内部文本"Amend"进行交互的一些按钮。但是,这在源代码中为<td><a onclick="return confirm(&#39;Are you sure you want to Amend this booking?&#39;);" id="MainContent_rptBookings_btnAmendBooking_0" class="btn_amend" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl01$btnAmendBooking&#39;,&#39;&#39;)">Amend</a></td>

所以这是我的循环代码

   Set tbl = ie.document.getElementsByTagName("table")(1)
    Set trs = tbl.getElementsByTagName("tr")

     For r = 0 To trs.Length - 1
        Set tds = trs(r).getElementsByTagName("td")
        'if no <td> then look for <th>
        If tds.Length = 0 Then Set tds = trs(r).getElementsByTagName("th")

        For c = 0 To tds.Length - 1
            If tds(c).innerText Like "471*" Then

                Storage(d, 0) = tds(c).innerText
                Storage(d, 1) = tds(c + 2).innerText
                Storage(d, 2) = tds(c + 4).innerText
                If tds(c + 6).innerText = "Amend" Then
                    Set Amen_BTN(d, 0) = tds(c + 6).****BUT WHAT GOES HERE!?*****
                    End If

                d = d + 1
            Debug.Print tds(c).innerText
            End If
        Next c
    Next r

那么在*****中允许我存储按钮以便稍后在Amen_BTN(0, 0).Click上使用,我希望这是一个可以理解的问题,

感谢您抽出宝贵时间阅读本文!

2 个答案:

答案 0 :(得分:1)

对于那些想要来源的人,

&#13;
&#13;
<input type="submit" name="ctl00$MainContent$Pager$btnNext" value="Next" id="MainContent_Pager_btnNext" Page="2" style="height:19px;width:40px;" />
<input type="submit" name="ctl00$MainContent$Pager$btnLast" value="Last" id="MainContent_Pager_btnLast" Page="2" style="height:19px;width:61px;" />&nbsp;&nbsp;Total pages:2
</div>

<div id="divResults" style="float: left">
  
    <table id="tblResults" >
        
   
        <thead><tr><th></th><th>Booking Ref</th><th>Pickup Location</th><th>Passenger Name</th><th>Destination Location</th><th>Date Time</th><th>Status</th><th colspan="3">Options</th></tr></thead>
      
         <tr>
             <td rowspan="3">
                 <button type="button" class="btn_view" onclick="ViewBookingDetails(4734464);">View</button>
             </td>
            <td rowspan="3"><div id="bookingID">4734464</div></td>            
            <td rowspan="3"><div id="pickupAddress"> ST LEONARDS WEST MARINA DEPOT, CLIFTONVILLE RD, HASTINGS, TN38 8AG</div></td>
            <td rowspan="3"><div id="passengerName"> STP HH970</div></td>
            <td rowspan="3">HASTINGS STATION PICK UP BY COLLEGE ENTRANCE, STATION APPROACH, HASTINGS, TN34 1BA</td>
            <td rowspan="3">10/03/2017 20:04</td>            
            <td rowspan="3"><span class="statusConfirmed">Confirmed</span></td>    
           
            <td><a onclick="return confirm(&#39;Are you sure you want to Amend this booking?&#39;);" id="MainContent_rptBookings_btnAmendBooking_0" class="btn_amend" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl01$btnAmendBooking&#39;,&#39;&#39;)">
             Amend</a></td>
         </tr>
            <tr><td><a id="MainContent_rptBookings_btnBookReturn_0" class="btn_return" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl01$btnBookReturn&#39;,&#39;&#39;)">Return</a></td></tr>                              
            <tr><td><a onclick="return confirm(&#39;Are you sure you want to Cancel this booking?&#39;);" id="MainContent_rptBookings_btnCancelBooking_0" class="btn_cancel_ASI" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl01$btnCancelBooking&#39;,&#39;&#39;)">
                Cancel</a></td></tr>
         
         
      
         <tr>
             <td rowspan="3">
                 <button type="button" class="btn_view" onclick="ViewBookingDetails(4734463);">View</button>
             </td>
            <td rowspan="3"><div id="bookingID">4734463</div></td>            
            <td rowspan="3"><div id="pickupAddress"> ST LEONARDS WEST MARINA DEPOT, CLIFTONVILLE RD, HASTINGS, TN38 8AG</div></td>
            <td rowspan="3"><div id="passengerName"> LTP HH970</div></td>
            <td rowspan="3">HASTINGS STATION PICK UP BY COLLEGE ENTRANCE, STATION APPROACH, HASTINGS, TN34 1BA</td>
            <td rowspan="3">09/03/2017 20:04</td>            
            <td rowspan="3"><span class="statusConfirmed">Confirmed</span></td>    
           
            <td><a onclick="return confirm(&#39;Are you sure you want to Amend this booking?&#39;);" id="MainContent_rptBookings_btnAmendBooking_1" class="btn_amend" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl02$btnAmendBooking&#39;,&#39;&#39;)">
             Amend</a></td>
         </tr>
            <tr><td><a id="MainContent_rptBookings_btnBookReturn_1" class="btn_return" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl02$btnBookReturn&#39;,&#39;&#39;)">Return</a></td></tr>                              
            <tr><td><a onclick="return confirm(&#39;Are you sure you want to Cancel this booking?&#39;);" id="MainContent_rptBookings_btnCancelBooking_1" class="btn_cancel_ASI" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl02$btnCancelBooking&#39;,&#39;&#39;)">
                Cancel</a></td></tr>
         
         
      
         <tr>
             <td rowspan="3">
                 <button type="button" class="btn_view" onclick="ViewBookingDetails(4734462);">View</button>
             </td>
            <td rowspan="3"><div id="bookingID">4734462</div></td>            
            <td rowspan="3"><div id="pickupAddress"> ST LEONARDS WEST MARINA DEPOT, CLIFTONVILLE RD, HASTINGS, TN38 8AG</div></td>
            <td rowspan="3"><div id="passengerName"> LTP HH970</div></td>
            <td rowspan="3">HASTINGS STATION PICK UP BY COLLEGE ENTRANCE, STATION APPROACH, HASTINGS, TN34 1BA</td>
            <td rowspan="3">08/03/2017 20:04</td>            
            <td rowspan="3"><span class="statusConfirmed">Confirmed</span></td>    
           
            <td><a onclick="return confirm(&#39;Are you sure you want to Amend this booking?&#39;);" id="MainContent_rptBookings_btnAmendBooking_2" class="btn_amend" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl03$btnAmendBooking&#39;,&#39;&#39;)">
             Amend</a></td>
         </tr>
            <tr><td><a id="MainContent_rptBookings_btnBookReturn_2" class="btn_return" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl03$btnBookReturn&#39;,&#39;&#39;)">Return</a></td></tr>                              
            <tr><td><a onclick="return confirm(&#39;Are you sure you want to Cancel this booking?&#39;);" id="MainContent_rptBookings_btnCancelBooking_2" class="btn_cancel_ASI" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl03$btnCancelBooking&#39;,&#39;&#39;)">
                Cancel</a></td></tr>
         
         
      
         <tr>
             <td rowspan="3">
                 <button type="button" class="btn_view" onclick="ViewBookingDetails(4734461);">View</button>
             </td>
            <td rowspan="3"><div id="bookingID">4734461</div></td>            
            <td rowspan="3"><div id="pickupAddress"> ST LEONARDS WEST MARINA DEPOT, CLIFTONVILLE RD, HASTINGS, TN38 8AG</div></td>
            <td rowspan="3"><div id="passengerName"> LTP HH970</div></td>
            <td rowspan="3">HASTINGS STATION PICK UP BY COLLEGE ENTRANCE, STATION APPROACH, HASTINGS, TN34 1BA</td>
            <td rowspan="3">07/03/2017 20:04</td>            
            <td rowspan="3"><span class="statusConfirmed">Confirmed</span></td>    
           
            <td><a onclick="return confirm(&#39;Are you sure you want to Amend this booking?&#39;);" id="MainContent_rptBookings_btnAmendBooking_3" class="btn_amend" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl04$btnAmendBooking&#39;,&#39;&#39;)">
             Amend</a></td>
         </tr>
            <tr><td><a id="MainContent_rptBookings_btnBookReturn_3" class="btn_return" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl04$btnBookReturn&#39;,&#39;&#39;)">Return</a></td></tr>                              
            <tr><td><a onclick="return confirm(&#39;Are you sure you want to Cancel this booking?&#39;);" id="MainContent_rptBookings_btnCancelBooking_3" class="btn_cancel_ASI" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl04$btnCancelBooking&#39;,&#39;&#39;)">
                Cancel</a></td></tr>
         
         
      
         <tr>
             <td rowspan="3">
                 <button type="button" class="btn_view" onclick="ViewBookingDetails(4734460);">View</button>
             </td>
            <td rowspan="3"><div id="bookingID">4734460</div></td>            
            <td rowspan="3"><div id="pickupAddress"> ST LEONARDS WEST MARINA DEPOT, CLIFTONVILLE RD, HASTINGS, TN38 8AG</div></td>
            <td rowspan="3"><div id="passengerName"> LTP HH970</div></td>
            <td rowspan="3">HASTINGS STATION PICK UP BY COLLEGE ENTRANCE, STATION APPROACH, HASTINGS, TN34 1BA</td>
            <td rowspan="3">06/03/2017 20:04</td>            
            <td rowspan="3"><span class="statusConfirmed">Confirmed</span></td>    
           
            <td><a onclick="return confirm(&#39;Are you sure you want to Amend this booking?&#39;);" id="MainContent_rptBookings_btnAmendBooking_4" class="btn_amend" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl05$btnAmendBooking&#39;,&#39;&#39;)">
             Amend</a></td>
         </tr>
            <tr><td><a id="MainContent_rptBookings_btnBookReturn_4" class="btn_return" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl05$btnBookReturn&#39;,&#39;&#39;)">Return</a></td></tr>                              
            <tr><td><a onclick="return confirm(&#39;Are you sure you want to Cancel this booking?&#39;);" id="MainContent_rptBookings_btnCancelBooking_4" class="btn_cancel_ASI" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl05$btnCancelBooking&#39;,&#39;&#39;)">
                Cancel</a></td></tr>
         
         
      
         <tr>
             <td rowspan="3">
                 <button type="button" class="btn_view" onclick="ViewBookingDetails(4713706);">View</button>
             </td>
            <td rowspan="3"><div id="bookingID">4713706</div></td>            
            <td rowspan="3"><div id="pickupAddress"> ST LEONARDS WEST MARINA DEPOT, CLIFTONVILLE RD, HASTINGS, TN38 8AG</div></td>
            <td rowspan="3"><div id="passengerName"> LTP HH970</div></td>
            <td rowspan="3">HASTINGS STATION PICK UP BY COLLEGE ENTRANCE, STATION APPROACH, HASTINGS, TN34 1BA</td>
            <td rowspan="3">03/03/2017 20:04</td>            
            <td rowspan="3"><span class="statusConfirmed">Confirmed</span></td>    
           
            <td></td>
         </tr>
            <tr><td><a id="MainContent_rptBookings_btnBookReturn_5" class="btn_return" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl06$btnBookReturn&#39;,&#39;&#39;)">Return</a></td></tr>                              
            <tr><td></td></tr>
         
         
      
         <tr>
             <td rowspan="3">
                 <button type="button" class="btn_view" onclick="ViewBookingDetails(4713705);">View</button>
             </td>
            <td rowspan="3"><div id="bookingID">4713705</div></td>            
            <td rowspan="3"><div id="pickupAddress"> ST LEONARDS WEST MARINA DEPOT, CLIFTONVILLE RD, HASTINGS, TN38 8AG</div></td>
            <td rowspan="3"><div id="passengerName"> LTP HH970</div></td>
            <td rowspan="3">HASTINGS STATION PICK UP BY COLLEGE ENTRANCE, STATION APPROACH, HASTINGS, TN34 1BA</td>
            <td rowspan="3">02/03/2017 20:04</td>            
            <td rowspan="3"><span class="statusConfirmed">Confirmed</span></td>    
           
            <td></td>
         </tr>
            <tr><td><a id="MainContent_rptBookings_btnBookReturn_6" class="btn_return" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl07$btnBookReturn&#39;,&#39;&#39;)">Return</a></td></tr>                              
            <tr><td></td></tr>
         
         
      
         <tr>
             <td rowspan="3">
                 <button type="button" class="btn_view" onclick="ViewBookingDetails(4713704);">View</button>
             </td>
            <td rowspan="3"><div id="bookingID">4713704</div></td>            
            <td rowspan="3"><div id="pickupAddress"> ST LEONARDS WEST MARINA DEPOT, CLIFTONVILLE RD, HASTINGS, TN38 8AG</div></td>
            <td rowspan="3"><div id="passengerName"> LTP HH970</div></td>
            <td rowspan="3">HASTINGS STATION PICK UP BY COLLEGE ENTRANCE, STATION APPROACH, HASTINGS, TN34 1BA</td>
            <td rowspan="3">01/03/2017 20:04</td>            
            <td rowspan="3"><span class="statusConfirmed">Confirmed</span></td>    
           
            <td></td>
         </tr>
            <tr><td><a id="MainContent_rptBookings_btnBookReturn_7" class="btn_return" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl08$btnBookReturn&#39;,&#39;&#39;)">Return</a></td></tr>                              
            <tr><td></td></tr>
         
         
      
         <tr>
             <td rowspan="3">
                 <button type="button" class="btn_view" onclick="ViewBookingDetails(4719527);">View</button>
             </td>
            <td rowspan="3"><div id="bookingID">4719527</div></td>            
            <td rowspan="3"><div id="pickupAddress"> CLIFTONVILLE WEST MARINA DEPOT, CLIFTONVILLE ROAD, ST LEONARDS ON SEA, EAST SUSSEX, TN38 8AG</div></td>
            <td rowspan="3"><div id="passengerName"> Andrew Hansen</div></td>
            <td rowspan="3">HASTINGS STATION PICK UP BY COLLEGE ENTRANCE, STATION APPROACH, HASTINGS, SEVENOAKS, TN34 1BA</td>
            <td rowspan="3">28/02/2017 21:10</td>            
            <td rowspan="3"><span class="statusUnknown">Amended</span></td>    
           
            <td></td>
         </tr>
            <tr><td><a id="MainContent_rptBookings_btnBookReturn_8" class="btn_return" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl09$btnBookReturn&#39;,&#39;&#39;)">Return</a></td></tr>                              
            <tr><td></td></tr>
         
         
      
         <tr>
             <td rowspan="3">
                 <button type="button" class="btn_view" onclick="ViewBookingDetails(4713703);">View</button>
             </td>
            <td rowspan="3"><div id="bookingID">4713703</div></td>            
            <td rowspan="3"><div id="pickupAddress"> ST LEONARDS WEST MARINA DEPOT, CLIFTONVILLE RD, HASTINGS, TN38 8AG</div></td>
            <td rowspan="3"><div id="passengerName"> Andrew Hanson</div></td>
            <td rowspan="3">HASTINGS STATION PICK UP BY COLLEGE ENTRANCE, STATION APPROACH, HASTINGS, TN34 1BA</td>
            <td rowspan="3">28/02/2017 20:00</td>            
            <td rowspan="3"><span class="statusCancelled">Cancelled</span></td>    
           
            <td></td>
         </tr>
            <tr><td><a id="MainContent_rptBookings_btnBookReturn_9" class="btn_return" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl10$btnBookReturn&#39;,&#39;&#39;)">Return</a></td></tr>                              
            <tr><td></td></tr>
         
         
      
         <tr>
             <td rowspan="3">
                 <button type="button" class="btn_view" onclick="ViewBookingDetails(4713702);">View</button>
             </td>
            <td rowspan="3"><div id="bookingID">4713702</div></td>            
            <td rowspan="3"><div id="pickupAddress"> ST LEONARDS WEST MARINA DEPOT, CLIFTONVILLE RD, HASTINGS, TN38 8AG</div></td>
            <td rowspan="3"><div id="passengerName"> Paul Kennard</div></td>
            <td rowspan="3">HASTINGS STATION PICK UP BY COLLEGE ENTRANCE, STATION APPROACH, HASTINGS, TN34 1BA</td>
            <td rowspan="3">27/02/2017 20:00</td>            
            <td rowspan="3"><span class="statusUnknown">Amended</span></td>    
           
            <td></td>
         </tr>
            <tr><td><a id="MainContent_rptBookings_btnBookReturn_10" class="btn_return" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl11$btnBookReturn&#39;,&#39;&#39;)">Return</a></td></tr>                              
            <tr><td></td></tr>
         
         
      
&#13;
&#13;
&#13;

答案 1 :(得分:1)

查看页面的html-snippet并阅读你的评论我终于明白你想要实现的目标。但这是IMO不可能的:(。

最后一个单元格具有以下内容:

<td>
    <a onclick="return confirm(&#39;Are you sure you want to Amend this booking?&#39;);" 
        id="MainContent_rptBookings_btnAmendBooking_0" 
        class="btn_amend" href="javascript:__doPostBack(&#39;ctl00$MainContent$rptBookings$ctl01$btnAmendBooking&#39;,&#39;&#39;)">
        Amend
    </a>
</td>

因此它包含一个带有java-script confirm调用的anchor元素。当用户确认修改时,将调用_doPostBack函数,接收导致post-back的控件名称,然后执行form的提交。因此整个页面在 POST 请求中发送到服务器,在服务器上对数据执行某些操作并返回结果页面。 it 表示某些服务器逻辑,它需要一些参数并返回结果。因此,当网格tblResults一次不提供多于一个项目的批量处理时,一次处理多个项目可能是不可能或不安全的。所以在这里你可以点击一个修改并让服务器处理请求,然后从服务器获取新数据并处理下一个等等。这是可以理解的吗? HTH

  

编辑

     
      
  • 如何使用VBA点击anchor元素:
  •   
     

此示例中使用了querySelector方法。我们指定了   选择器到特定的anchor元素,例如像这样:

div[id='divResults'] table[id='tblResults'] tbody td 
a[id='MainContent_rptBookings_btnAmendBooking_0']

完整的代码可能看起来像这样,HTH。

Option Explicit

' Add reference to Microsoft Internet Controls (SHDocVw)
' Add reference to Microsoft HTML Object Library

Sub ClickOnAmendBooking()

    Dim ie As SHDocVw.InternetExplorer
    Dim doc As MSHTML.HTMLDocument
    Dim url As String

    url = "file:///c:/Temp/StackOverflow/html/"
    Set ie = New SHDocVw.InternetExplorer
    ie.Visible = True
    ie.navigate url & "Chris_livermore.html"

    While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE: DoEvents: Wend

    Set doc = ie.document

    Dim btnAmendBooking As HTMLAnchorElement
    Set btnAmendBooking = doc.querySelector("div[id='divResults'] table[id='tblResults'] tbody td a[id='MainContent_rptBookings_btnAmendBooking_0']")

    If Not btnAmendBooking Is Nothing Then
        btnAmendBooking.Click
    Else
        MsgBox "btnAmendBooking was not found on webpage"
    End If

    ie.Quit
    Set ie = Nothing
End Sub
  

结果:   enter image description here