如何使用 getElementsByTagName with <td> with overflow: hidden on VBA 获取特定值?

问题描述

我试图从我的工作场所使用的网站获取一些特定数据,但是,我无法弄清楚如何获取该特定数据。以下是来自网站的代码片段:

<tr class="outboundplanAltRowStyle">
<td class="outboundplanHour" style="height:25px;width:40px;white-space:Nowrap;">11:00</td>
 <td onmouSEOver="this.className='outboundplanHover'" onmouSEOut="this.className=''" 
     onclick="cellClicked(1019543,14)" 
     style="height:25px;width:150px;white-space:Nowrap;" class="">
<table class="outboundplan_PREBOOKED" style="width: 200px; table-layout: fixed" cellpadding="0px" cellspacing="0px">
   <tbody><tr>
    <td title="Purchase Order / Status" class="outboundCell"> 325839 / PREBOOKED</td></tr>
    <tr><td title="Subcontractor Name / Load Numbers " 
               class="outboundCell">Tesco FM /  - </td></tr>
    <tr><td title="Planned Destinations " class="outboundCell" 
       style="overflow: hidden"> 39019 (NDC Teresin Tesco)&nbsp;</td></tr>
    <tr><td title="Status Date" class="outboundCell">28.01.2021 12:02&nbsp;</td></tr></tbody>
</table></td>
<td onmouSEOver="this.className='outboundplanHover'" onmouSEOut="this.className=''" 
onclick="cellClicked(1019544,14)" 
style="height:25px;width:150px;white-space:Nowrap;" class="">
<table class="outboundplan_CONFIRMED" style="width: 200px; table-layout: fixed" cellpadding="0px" cellspacing="0px"><tbody>

        <tr><td title="Purchase Order / Status" class="outboundCell"> 325676 / CONFIRMED</td>
        <td title="Released" class="outboundCell3">R</td></tr>
        <tr><td colspan="2" title="Subcontractor Name / Truck No / Trailer No " class="outboundCell">Tesco ESA  / 4H5 5484 / 4H2 6597</td></tr>
        <tr><td colspan="2" title="Load Numbers / Store Names" class="outboundCell"> 25060,250601,250602 /  Ceska Trebova 3k,HM USTI NO 3k,HM VYSOKE MYTO</td></tr><tr><td title="Status Date" class="outboundCell">28.01.2021 22:29</td>
        <td title="25060-31-B-HV" class="outboundCell2">32 DC2</td>
        </tr></tbody></table></td><td style="height:25px;width:150px;white-space:Nowrap;"><table class="outboundplanopen" style="width: 200px; table-layout: fixed" cellpadding="0px" cellspacing="0px"><tbody><tr><td>&nbsp;</td></tr>

        <tr><td>(open)</td></tr>
        <tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr></tbody></table></td><td style="height:25px;width:150px;white-space:Nowrap;"><table class="outboundplanopen" style="width: 200px; table-layout: fixed" cellpadding="0px" cellspacing="0px"><tbody><tr><td>&nbsp;</td></tr>
        <tr><td>(open)</td></tr>
        <tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr></tbody></table></td><td style="height:25px;width:150px;white-space:Nowrap;"><table class="outboundplanopen" style="width: 200px; table-layout: fixed" cellpadding="0px" cellspacing="0px"><tbody><tr><td>&nbsp;</td></tr>
        <tr><td>(open)</td></tr>
        <tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr></tbody></table></td><td style="height:25px;width:150px;white-space:Nowrap;"><table class="outboundplanopen" style="width: 200px; table-layout: fixed" cellpadding="0px" cellspacing="0px"><tbody><tr><td>&nbsp;</td></tr>
        <tr><td>(open)</td></tr>
        <tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr></tbody></table></td>
                        </tr>

我试图从 class="outboundCell"> 325839 / PREBOOKED

Sub WebNavigate()

im objIE As Variant
Dim WebSite As Variant
Dim Element As Variant
Dim i As Variant
    Set objIE = CreateObject("InternetExplorer.Application")
    WebSite = "https://*******.aspx"
    
    Sleep 1000
    
    With objIE
        .Visible = True
        .Navigate WebSite
        do while .Busy Or .ReadyState <> 4
            DoEvents
        Loop

        Sleep 1000
               
        objIE.Document.all.Item("ctl00$Content$Login1$UserName").Value = "*****"
        objIE.Document.all.Item("ctl00$Content$Login1$Password").Value = "*****"
        
        Sleep 1000
        
        objIE.Document.getElementsByName("ctl00$Content$Login1$LoginButton")(0).Click
        
        Sleep 1000
               
        End With
         If objIE.ReadyState = 4 Then
        objIE.Navigate "https://******"
        End If
        
        Sleep 1000
        
        
        do while objIE.Busy Or objIE.ReadyState <> 4
            DoEvents
        Loop
        
        Sleep 1000
        
        If objIE.ReadyState = 4 Then
        Dim getTodayDay As Variant
        getTodayDay = Day(Now) '- 1
        objIE.Document.getElementsByClassName("CalendarDayNo")(getTodayDay).Click
        End If
        
        Sleep 1000
        
        do while objIE.Busy Or objIE.ReadyState <> 4
            DoEvents
        Loop
        
        Sleep 1000
        
        If objIE.ReadyState = 4 Then

Dim text As String
Dim Data As Variant
text = " 325839 / PREBOOKED"
Set Data =objIE.Document.getElementsByClassName("outboundCell")(text).

Sleep 1000
do while objIE.Busy Or objIE.ReadyState <> 4
            DoEvents
        Loop

        
        End If

End Sub

我故意省略了站点名称登录名和密码。我编写的代码片段成功地将我带到了相关页面,其中写入了信息,但是,我无法弄清楚如何获取它。这就是 Set Data =objIE.Document.getElementsByClassName("outboundCell")(text). 部分没有完成的原因。

解决方法

使用 attribute = value css 选择器通过 Title 属性的值定位

 objIE.Document.querySelector("[Title='Planned Destinations']").innerText

在此处阅读有关属性选择器的信息:https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors

多个匹配的节点,用querySelectorAll收集一个nodeList并循环

Dim nodes As Object,i As Long

Set nodes = objIE.Document.querySelectorAll("[Title='Planned Destinations']")

For i = 0 To nodes.Length -1

    If nodes.item(i).innerText = " 325839 / PREBOOKED" Then
        'Do something here
        Exit For
    End If

Next

如果您只知道要匹配的所需字符串的一部分,则可以将 LIKE 与通配符或 Instr 一起使用。如果您无法预测要匹配的字符串/子字符串,那么您将需要查看位置匹配(无论是绝对 - 索引;还是相对 - 与其他元素相关)。


来自@TimWilliams 的附加链接:

querySelector and querySelectorAll vs getElementsByClassName and getElementById in JavaScript