简体   繁体   中英

Excel VBA extracting aria-label value

I'm trying to extract the text inside the aria-label attribute but what I have doesn't seem to be working. I'm able to extract href values using the same code so I thought it might also work. Any help would be appreciated.

The URL I used is https://www.facebook.com/marketplace/item/328932021226229 截图:

 <div class="_3-8z"> <div> <span class="_3ziq">Seller Information</span> <div class="clearfix" direction="left"> <div class="_ohe lfloat"> <div> <a class="img _8o _8t" aria-label="John Smith, View seller profile" href="#" data-hovercard="/ajax/hovercard/user.php?id=100002935356728&amp;extragetparams=%7B%22hc_location%22%3A%22marketplace_hovercard%22%2C%22existingThreadID%22%3Anull%2C%22forSaleItemID%22%3A%22328932021226229%22%2C%22name%22%3A%22Zsigmond%20Lali%22%7D" modalProps="[object Object]" profileID="100002935356728" resource="[object Object]"> 

    Sub Macro2()

marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
    On Error Resume Next    ' sometimes more web pages are counted than are open
    my_url = objShell.Windows(x).document.Location
    my_title = objShell.Windows(x).document.Title

    If my_title Like "Marketplace" & "*" Then 'compare to find if the desired web page is already open
        Set IE = objShell.Windows(x)
        marker = 1
        Exit For
    Else
    End If
Next

        Dim aNodeList As Object, i As Long
        Set aNodeList = IE.document.querySelectorAll(".img _8o _8t[aria-label]")
        For i = 0 To aNodeList.Length - 1
            ActiveSheet.Cells(i + 2, 2) = aNodeList.Item(i)
        Next

End Sub

Your snippet link HTML does not appear in the link, at least as I see it. Also, your CSS selector has the wrong class selector for the shown snippet if you are after the shown aria-label .

This

._3cgd[aria-label]

looks for an element with class name ._3cgd having an aria-label attribute. There isn't one present in your snippet.

I would expect, but cannot properly test for reasons above, that you could use getAttribute , failing that split the .outerHTML of the target element.

The below is a more generalised selector based on your snippet shown. You may need to adjust for your HTML. The getAttribute produced null with just your snippet but I am unsure if would behave differently with live page as the syntax is correct. The outerHTML splitting returns John Smith, View seller profile

With IE.document.querySelector("a[class='img _8o _8t'][profileid='100002935356728']")
   Debug.Print .getAttribute("aria-label")
   Debug.Print Split(Split(.outerHTML, "aria-label=" & Chr$(34))(1), Chr$(34))(0)
End With

I am only using querySelector and targeting with profileid , to be more general for all matched class with aria-label:

Dim eles As Object, i As Long
Set eles = IE.document.querySelectorAll("a[class='img _8o _8t'][aria-label]")
For i = 0 To eles.Length - 1
    With eles.item(i)
        Debug.Print .getAttribute("aria-label")
        Debug.Print Split(Split(.outerHTML, "aria-label=" & Chr$(34))(1), Chr$(34))(0)
    End With
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM