如何使用带有 <td> 的 getElementsByTagName 获取特定值并溢出:隐藏在 VBA 上?

How to get specific value with 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< class 获取值“39019 (NDC Teresin Tesco)”到 Sheet1 上的单元格 A1 中,但是,我不能正确定义它。这是我到目前为止得到的代码:

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 一起使用。如果您无法预测要匹配的 string/substring,那么您将需要查看位置匹配(无论是绝对 - 索引;还是相对 - 相对于其他元素)。


@TimWilliams 的其他 link:

querySelector and querySelectorAll vs getElementsByClassName and getElementById in JavaScript