Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

excel - How to get specific value with getElementsByTagName with <td> with overflow: hidden on VBA?

Im trying to get some specific data from a website my workplace uses, however, I cannot figure out on how to get that specific data. Here is a snippet of the code from the website:

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

Im trying to get the value "39019 (NDC Teresin Tesco)" from the class="outboundCell"> 325839 / PREBOOKED< class into cell A1 on Sheet1, however, i cannot define it properly. Here is the code ive got so far:

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

I intentionally left out the name of the site and login name and password. The snippet of code I worte successfully takes me to the relevant page, where the information is written, however, I just cant figure out on how to get it. That is why the Set Data =objIE.Document.getElementsByClassName("outboundCell")(text). part isn't finished.

question from:https://stackoverflow.com/questions/65946677/how-to-get-specific-value-with-getelementsbytagname-with-td-with-overflow-hid

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use an attribute = value css selector to target the Title attribute by its value

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

Read about attribute selectors here: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors

More than one matching node, gather a nodeList with querySelectorAll and loop

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

You can use LIKE with wildcard(s), or Instr, if you only know part of the desired string to match on. If you cannot predict the string/substring to match on then you will need to look at positional matching (whether absolute - index; or, relative - in relation to other element(s)).


Additional link from @TimWilliams:

querySelector and querySelectorAll vs getElementsByClassName and getElementById in JavaScript


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...