Results 1 to 29 of 29

Thread: [RESOLVED] Getting CLASS from GetElementsByTagName

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    255

    Resolved [RESOLVED] Getting CLASS from GetElementsByTagName

    I have the code below that was provided by paul but it is not returning the collection. paul initially set it up so I could search through the collection, which I do with GetElement, but I have this function GetElements that returns the collection, or at leas it's supposed to. I tried to doo AllClasses.All but VB does not like that. What am I missing, or doing wrong?

    Code:
    Error occured in [GetElement] for the following reason Unable to cast object of type 'WhereEnumerableIterator`1[System.Windows.Forms.HtmlElement]' to type 'System.Windows.Forms.HtmlElementCollection'.
    Code:
    Public Function GetElements(ByVal nElement As Integer, ByVal szElementName As String) As HtmlElementCollection
    
        Dim HTMLeleCol As HtmlElementCollection
        Dim nCnt As Integer
    
        Try
    
            If (GetDebug()) Then
                Debug.Print("clsIIE [GetElement Enter]")
            End If
    
            If (GetAbort()) Then
                GetElements = Nothing
                Exit Function
            End If
    
            'If (IsObjectConnected(WebBrowser1.Document) = False) Then
            '    GetElement = Nothing
            '    Exit Function
            'End If
    
    
            nCnt = 0
            HTMLeleCol = Nothing
            'loop until the home page shows up
            Do While (HTMLeleCol Is Nothing And GetAbort() = False And m_BrowserComplete = False And nCnt < 25)
    
                Application.DoEvents()
    
                Select Case nElement
    
                    'Case ENUM_ELEMENT.eELEMENT_ID
                    '    HTMLele = WebBrowser1.Document.GetElementById(szElementName)
    
                    Case ENUM_ELEMENT.eELEMENT_CLASS
                        ' return first by className
                        HTMLeleCol = WebBrowser1.Document.GetElementsByTagName("*")
                        'Dim allClasses As IEnumerable(Of HtmlElement) = HTMLcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("ClassName") = tagOrClassName)
                        Dim allClasses As IEnumerable(Of HtmlElement) = HTMLeleCol.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("ClassName") = szElementName)
                        Return allClasses
    
                    Case ENUM_ELEMENT.eELEMENT_TAG
                        Return WebBrowser1.Document.GetElementsByTagName(szElementName)
    
                    Case ENUM_ELEMENT.eELEMENT_NAME
                        ' return first by Name
                        HTMLeleCol = WebBrowser1.Document.GetElementsByTagName("*")
                        ' this should find only one unique element by name
                        Return HTMLeleCol.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("Name") = szElementName)
    
                        'Case ENUM_ELEMENT.eELEMENT_ID_TITLE
                        'HTMele = WebBrowser1.Document.getelementidbytitle
    
                        'Case ENUM_ELEMENT.eELEMENT_INDEX
    
                End Select
                Thread.Sleep(200)
                nCnt = nCnt + 1
            Loop
    
            GetElements = HTMLeleCol
    
            If (GetAbort()) Then
                HTMLeleCol = Nothing
                GetElements = Nothing
            End If
    
            If (GetDebug()) Then
                Debug.Print("clsIIE [GetElement Exit]")
            End If
    
        Catch ex As Exception
            Dim szErr As String
            szErr = "Error occured in [GetElement] for the following reason " + ex.Message
            If (GetDebug()) Then
                Debug.Print(szErr)
                'gClsExcel.ExcelWriteErrorLog("MenuSelect", szErr)
            End If
            HTMLeleCol = Nothing
            GetElements = Nothing
        End Try
    
     
    End Function
    here is the original code paul provided. Notice that the only difference is I'm returning a single instance of the item.

    Code:
    Public Function GetElement(ByVal nElement As Integer, ByVal szElementName As String) As HtmlElement
    
        Dim HTMLcoll As HtmlElementCollection
        Dim ele As HtmlElement
        Dim nCnt As Integer
    
        Try
    
            If (GetDebug()) Then
                Debug.Print("clsIIE [GetElement Enter]")
            End If
    
            If (GetAbort()) Then
                GetElement = Nothing
                Exit Function
            End If
    
            'If (IsObjectConnected(WebBrowser1.Document) = False) Then
            '    GetElement = Nothing
            '    Exit Function
            'End If
    
    
            Application.DoEvents()
    
            Select Case nElement
                Case ENUM_ELEMENT.eELEMENT_ID
                    ' return by ID
                    Return WebBrowser1.Document.GetElementById(szElementName)
                Case ENUM_ELEMENT.eELEMENT_CLASS
                    ' return first by className
                    HTMLcoll = WebBrowser1.Document.GetElementsByTagName("*")
                    'Dim allClasses As IEnumerable(Of HtmlElement) = HTMLcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("ClassName") = tagOrClassName)
                    Dim allClasses As IEnumerable(Of HtmlElement) = HTMLcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("ClassName") = szElementName)
                    ' find index to return by searching allClasses for some identifying attribute = szElementName
                    For Each ele In allClasses
                        If (InStr(ele.OuterHtml, szElementName) <> 0) Then
                            Exit For
                        End If
                    Next
                    Return ele
                Case ENUM_ELEMENT.eELEMENT_TAG
                    ' return first by tagName
                    'HTMLcoll = WebBrowser1.Document.GetElementsByTagName(tagOrClassName)
                    HTMLcoll = WebBrowser1.Document.GetElementsByTagName(szElementName)
                    ' find index to return by searching HTMcoll for some identifying attribute = szElementName
                    For Each ele In HTMLcoll
                        If (InStr(ele.Name, szElementName) <> 0) Then
                            Exit For
                        End If
                    Next
                    Return ele
                Case ENUM_ELEMENT.eELEMENT_NAME
                    ' return first by Name
                    HTMLcoll = WebBrowser1.Document.GetElementsByTagName("*")
                    ' this should find only one unique element by name
                    Return HTMLcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("Name") = szElementName).First
            End Select
    
            Application.DoEvents()
    
    
            'nCnt = 0
            'HTMLele = Nothing
            ''loop until the home page shows up
            'Do While (HTMLele Is Nothing And GetAbort() = False And m_BrowserComplete = False And nCnt < 25)
    
            '    Application.DoEvents()
    
            '    Select Case nElement
    
            '        Case ENUM_ELEMENT.eELEMENT_ID
            '            HTMLele = WebBrowser1.Document.GetElementById(szElementName)
    
            '            'Case ENUM_ELEMENT.eELEMENT_CLASS
            '            '    HTMLeleCol = WebBrowser1.Document.GetElementByClassName(szElementName)
    
            '            'Case ENUM_ELEMENT.eELEMENT_TAG
            '            '    HTMLeleCol = WebBrowser1.Document.GetElementsByTagName(szElementName)
    
            '            'Case ENUM_ELEMENT.eELEMENT_NAME
            '            'HTMele = WebBrowser1.Document.GetElementsByName(szElementName)
    
            '            'Case ENUM_ELEMENT.eELEMENT_ID_TITLE
            '            'HTMele = WebBrowser1.Document.getelementidbytitle
    
            '            'Case ENUM_ELEMENT.eELEMENT_INDEX
    
            '    End Select
            '    nCnt = nCnt + 1
            'Loop
    
            GetElement = Nothing
    
            If (GetAbort()) Then
                HTMLcoll = Nothing
                GetElement = Nothing
            End If
    
            If (GetDebug()) Then
                Debug.Print("clsIIE [GetElement Exit]")
            End If
    
        Catch ex As Exception
            Dim szErr As String
            szErr = "Error occured in [GetElement] for the following reason " + ex.Message
            If (GetDebug()) Then
                Debug.Print(szErr)
                'gClsExcel.ExcelWriteErrorLog("MenuSelect", szErr)
            End If
            HTMLcoll = Nothing
            GetElement = Nothing
        End Try
    
        'System.Runtime.InteropServices.Marshal.ReleaseComObject(HTMLobj)
    
    End Function

    EDIT: All the classes are there, the collections just won't return without an error.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,103

    Re: Getting CLASS from GetElementsByTagName

    LINQ queries always return their own LINQ-specific types. They won't magically produce a type specific to CodeDOM, like HtmlElementCollection. LINQ provides a ToList method to generate a List(Of T), but any collection type more specific than that, you must create yourself and populate from the LINQ results. In the case of HtmlElementCollection, I'm not sure that's possible, as I'm not sure it has any public members for doing that. Maybe it does and I was looking at the documentation for the wrong type but, if it doesn't, you have two main options:

    1. Change the return type of your method to IEnumerable or, preferably, IEnumerable(Of HtmlElement). The former is already implemented by the HtmlElementCollection and any LINQ query result. The latter is also implemented by any LINQ query results but not by HtmlElementCollecion, which you would have to call .Cast(Of HtmlElement)() on. Both those types allow you to enumerate the list, which basically means use a For Each loop over it. IEnumerable would require you to cast each item as the required type, while the generic version will expose items as that type inherently.

    2. Change the return type to IList(Of HtmlElement) or List(Of HtmlElement). That would enabled random access to the results, rather than just enumeration. You would call .ToList() on a LINQ query result and .Cast(Of HtmlElement)().ToList() on an HtmlElementCollection.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    255

    Re: Getting CLASS from GetElementsByTagName

    Quote Originally Posted by jmcilhinney View Post
    LINQ queries always return their own LINQ-specific types. They won't magically produce a type specific to CodeDOM, like HtmlElementCollection. LINQ provides a ToList method to generate a List(Of T), but any collection type more specific than that, you must create yourself and populate from the LINQ results. In the case of HtmlElementCollection, I'm not sure that's possible, as I'm not sure it has any public members for doing that. Maybe it does and I was looking at the documentation for the wrong type but, if it doesn't, you have two main options:

    1. Change the return type of your method to IEnumerable or, preferably, IEnumerable(Of HtmlElement). The former is already implemented by the HtmlElementCollection and any LINQ query result. The latter is also implemented by any LINQ query results but not by HtmlElementCollecion, which you would have to call .Cast(Of HtmlElement)() on. Both those types allow you to enumerate the list, which basically means use a For Each loop over it. IEnumerable would require you to cast each item as the required type, while the generic version will expose items as that type inherently.

    2. Change the return type to IList(Of HtmlElement) or List(Of HtmlElement). That would enabled random access to the results, rather than just enumeration. You would call .ToList() on a LINQ query result and .Cast(Of HtmlElement)().ToList() on an HtmlElementCollection.
    Thanks jmc. Can you confirm that I setup the code per your suggestion corrrectly?

    Code:
                        Case ENUM_ELEMENT.eELEMENT_CLASS
                            ' return first by className
                            HTMLeleCol = WebBrowser1.Document.GetElementsByTagName("*")
                            'Dim allClasses As IEnumerable(Of HtmlElement) = HTMLcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("ClassName") = tagOrClassName)
                            Dim allClasses As IEnumerable(Of HtmlElement) = HTMLeleCol.Cast(Of HtmlElement)().ToList.Where(Function(el As HtmlElement) el.GetAttribute("ClassName") = szElementName)
                            Return allClasses

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,093

    Re: Getting CLASS from GetElementsByTagName

    That’s not correct. The function I gave you returns a single HtmlElement. You seem to want it to return single elements and collections of elements…

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,093

    Re: Getting CLASS from GetElementsByTagName

    The question is, once you have your collection of elements by class name, or your collection of elements by tag name, how are you then going to identify the correct element?

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,093

    Re: Getting CLASS from GetElementsByTagName

    Your GetElements function should return an iEnumerable(Of HtmlElement) is what jmcilhinney was telling you.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    255

    Re: Getting CLASS from GetElementsByTagName

    Quote Originally Posted by .paul. View Post
    That’s not correct. The function I gave you returns a single HtmlElement. You seem to want it to return single elements and collections of elements…
    I was trying to use what you gave as the single return element, and that works, but also add a Collection and thought that just adding what was suggested would be good enough. Can you share how it would be?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    255

    Re: Getting CLASS from GetElementsByTagName

    Quote Originally Posted by .paul. View Post
    The question is, once you have your collection of elements by class name, or your collection of elements by tag name, how are you then going to identify the correct element?
    There are label identifiers in the class for the item I want to locate, but they are not directly visible so I will need to locate and then adjust code as needed. There are about 6 of the same type of classes that will be collected, each has a unique label for them.

    Hope that helps?

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    255

    Re: Getting CLASS from GetElementsByTagName

    Quote Originally Posted by .paul. View Post
    Your GetElements function should return an iEnumerable(Of HtmlElement) is what jmcilhinney was telling you.
    OK. DO I replace my HtmlElementsCollection with iEnumerable(Of HtmlElement)?

    Code:
     Public Function GetElements(ByVal nElement As Integer, ByVal szElementName As String) As iEnumerable(Of HtmlElement)

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,093

    Re: Getting CLASS from GetElementsByTagName

    Code:
    Public Function GetElements(ByVal nElement As Integer, ByVal szElementName As String) As IEnumerable(Of HtmlElement)
        Dim HTMcoll As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("*")
        Dim allClasses As IEnumerable(Of HtmlElement) = HTMcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("ClassName") = szElementName)
        Return allClasses
    End Function

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    255

    Re: Getting CLASS from GetElementsByTagName

    Quote Originally Posted by .paul. View Post
    Code:
    Public Function GetElements(ByVal nElement As Integer, ByVal szElementName As String) As IEnumerable(Of HtmlElement)
        Dim HTMcoll As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("*")
        Dim allClasses As IEnumerable(Of HtmlElement) = HTMcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("ClassName") = szElementName)
        Return allClasses
    End Function
    EDIT: Looks like the eELEMENT_TAG is a possible error according to the VB warning. What would change here?

    Code:
    Public Function GetElements(ByVal nElement As Integer, ByVal szElementName As String) As IEnumerable(Of HtmlElement)
    
            Dim HTMLIECol As IEnumerable(Of HtmlElement)
    Code:
                        Case ENUM_ELEMENT.eELEMENT_TAG
                            HTMLIECol = WebBrowser1.Document.GetElementsByTagName(szElementName)
                            Return HTMLIECol



    Thanks paul. One question. I don't see any difference between the original code you provided and this one. Where is the difference, or am I missing something? So what is different that it would not return the classes, was it just the ( As IEnumerable(Of HtmlElement))?


    Original code, first code provided.

    Code:
       Public Function GetElement(ByVal nElement As Integer, ByVal szElementName As String) As HtmlElement
    
           Dim HTMLcoll As HtmlElementCollection
           Dim ele As HtmlElement
           Dim nCnt As Integer
    
           Try
    
               If (GetDebug()) Then
                   Debug.Print("clsIIE [GetElement Enter]")
               End If
    
               If (GetAbort()) Then
                   GetElement = Nothing
                   Exit Function
               End If
    
               'If (IsObjectConnected(WebBrowser1.Document) = False) Then
               '    GetElement = Nothing
               '    Exit Function
               'End If
    
    
               Application.DoEvents()
    
               Select Case nElement
                   Case ENUM_ELEMENT.eELEMENT_ID
                       ' return by ID
                       Return WebBrowser1.Document.GetElementById(szElementName)
                   Case ENUM_ELEMENT.eELEMENT_CLASS
                       ' return first by className
                       HTMLcoll = WebBrowser1.Document.GetElementsByTagName("*")
                       'Dim allClasses As IEnumerable(Of HtmlElement) = HTMLcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("ClassName") = tagOrClassName)
                       Dim allClasses As IEnumerable(Of HtmlElement) = HTMLcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("ClassName") = szElementName)
                       ' find index to return by searching allClasses for some identifying attribute = szElementName
                       For Each ele In allClasses
                           If (InStr(ele.OuterHtml, szElementName) <> 0) Then
                               Exit For
                           End If
                       Next
                       Return ele
                   Case ENUM_ELEMENT.eELEMENT_TAG
                       ' return first by tagName
                       'HTMLcoll = WebBrowser1.Document.GetElementsByTagName(tagOrClassName)
                       HTMLcoll = WebBrowser1.Document.GetElementsByTagName(szElementName)
                       ' find index to return by searching HTMcoll for some identifying attribute = szElementName
                       For Each ele In HTMLcoll
                           If (InStr(ele.Name, szElementName) <> 0) Then
                               Exit For
                           End If
                       Next
                       Return ele
                   Case ENUM_ELEMENT.eELEMENT_NAME
                       ' return first by Name
                       HTMLcoll = WebBrowser1.Document.GetElementsByTagName("*")
                       ' this should find only one unique element by name
                       Return HTMLcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("Name") = szElementName).First
               End Select
    
               Application.DoEvents()

    New code provided. Below the 1st code provided. I did change HTMLcoll to HTMLeleCol, but that was the only change.
    Code:
                            'Dim allClasses As IEnumerable(Of HtmlElement) = HTMLcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("ClassName") = tagOrClassName)
                            Dim allClasses As IEnumerable(Of HtmlElement) = HTMLeleCol.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("ClassName") = szElementName)
    
                            Dim allClasses As IEnumerable(Of HtmlElement) = HTMLcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("ClassName") = szElementName)
    Not doubting, just trying to understand.
    Last edited by FunkMonkey; May 9th, 2025 at 10:11 PM.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,093

    Re: Getting CLASS from GetElementsByTagName

    You need to declare a variable for the HtmlElementCollection, which is not the same as the return type of the function, which is IEnumerable(Of HtmlElement)
    WebBrowser1.Document.GetElementsByTagName returns an HtmlElementCollection. If you use LINQ Where to refine that, it returns IEnumerable(Of HtmlElement)

    Code:
    For Each ele In HTMLcoll 'ele only has scope within the loop
        If (InStr(ele.Name, szElementName) <> 0) Then
             Exit For
        End If
    Next
    Return ele ' equals nothing, not even declared

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    255

    Re: Getting CLASS from GetElementsByTagName

    Quote Originally Posted by .paul. View Post
    You need to declare a variable for the HtmlElementCollection, which is not the same as the return type of the function, which is IEnumerable(Of HtmlElement)
    WebBrowser1.Document.GetElementsByTagName returns an HtmlElementCollection. If you use LINQ Where to refine that, it returns IEnumerable(Of HtmlElement)

    Code:
    For Each ele In HTMLcoll 'ele only has scope within the loop
        If (InStr(ele.Name, szElementName) <> 0) Then
             Exit For
        End If
    Next
    Return ele ' equals nothing, not even declared
    I guess I need to understand what you mean by LINQ. I see it means Language Integrated Query, but it is beyond me at the moment. What would the LINQ code look like for a GetElementsByTagName?

    Code:
    Public Function GetElements(ByVal nElement As Integer, ByVal szElementName As String) As IEnumerable(Of HtmlElement)
    
        Dim HTMLeleCol As HtmlElementCollection
        Dim HTMLIECol As IEnumerable(Of HtmlElement)
        Dim nCnt As Integer
    
        Try
    
            If (GetDebug()) Then
                Debug.Print("clsIIE [GetElement Enter]")
            End If
    
            If (GetAbort()) Then
                GetElements = Nothing
                Exit Function
            End If
    
            If (IsObjectConnected(WebBrowser1.Document) = False) Then
                GetElements = Nothing
                Exit Function
            End If
    
    
            nCnt = 0
            HTMLeleCol = Nothing
            'loop until the home page shows up
            Do While (HTMLeleCol Is Nothing And GetAbort() = False And m_BrowserComplete = False And nCnt < 25)
    
                Application.DoEvents()
    
                Select Case nElement
    
                    'Case ENUM_ELEMENT.eELEMENT_ID
                    '    HTMLele = WebBrowser1.Document.GetElementById(szElementName)
    
                    Case ENUM_ELEMENT.eELEMENT_CLASS
                        ' return first by className
                        HTMLeleCol = WebBrowser1.Document.GetElementsByTagName("*")
                        Dim allClasses As IEnumerable(Of HtmlElement) = HTMLeleCol.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("ClassName") = szElementName)
                        Return allClasses
    
                    Case ENUM_ELEMENT.eELEMENT_TAG
                        HTMLIECol = WebBrowser1.Document.GetElementsByTagName(szElementName)
                        Return HTMLIECol
    
                    Case ENUM_ELEMENT.eELEMENT_NAME
                        ' return first by Name
                        HTMLeleCol = WebBrowser1.Document.GetElementsByTagName("*")
                        ' this should find only one unique element by name
                        Return HTMLeleCol.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("Name") = szElementName)
    
                        'Case ENUM_ELEMENT.eELEMENT_ID_TITLE
                        'HTMele = WebBrowser1.Document.getelementidbytitle
    
                        'Case ENUM_ELEMENT.eELEMENT_INDEX
    
                End Select
                Thread.Sleep(200)
                nCnt = nCnt + 1
            Loop
    
            GetElements = HTMLeleCol
    
            If (GetAbort()) Then
                HTMLeleCol = Nothing
                GetElements = Nothing
            End If
    
            If (GetDebug()) Then
                Debug.Print("clsIIE [GetElement Exit]")
            End If
    
        Catch ex As Exception
            Dim szErr As String
            szErr = "Error occured in [GetElement] for the following reason " + ex.Message
            If (GetDebug()) Then
                Debug.Print(szErr)
                'gClsExcel.ExcelWriteErrorLog("MenuSelect", szErr)
            End If
            HTMLeleCol = Nothing
            GetElements = Nothing
        End Try
    
    End Function

  14. #14

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    255

    Re: Getting CLASS from GetElementsByTagName


  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    255

    Re: Getting CLASS from GetElementsByTagName

    paul. The code provided earlier does not seem to work for returning a single class as I keep getting Nothing. Below is the snapshot of the HTML code I'm working with.

    The Virtual Container
    Name:  Screenshot 2025-05-11 181555.jpg
Views: 670
Size:  9.1 KB


    The "ColumnValue EvenViewRecord". I have tried to use ELEMENT_CLASS and comes up with Nothing. In case the picture does not show up. This class is below the mentioned class with the list of items in the row with an "onclick" for the outerHTML (I think it's outer, hard to remember)

    Name:  Screenshot 2025-05-11 182011.jpg
Views: 569
Size:  14.0 KB
    Code:
        Public Function GetElement(ByVal nElement As Integer, ByVal szElementName As String) As HtmlElement
    
            Dim HTMLcoll As HtmlElementCollection
            Dim ele As HtmlElement
            Dim nCnt As Integer
    
            Try
    
                If (GetDebug()) Then
                    Debug.Print("clsIIE [GetElement Enter]")
                End If
    
                If (GetAbort()) Then
                    GetElement = Nothing
                    Exit Function
                End If
    
                If (IsObjectConnected(WebBrowser1.Document) = False) Then
                    GetElement = Nothing
                    Exit Function
                End If
    
    
                Application.DoEvents()
    
                Select Case nElement
                    Case ENUM_ELEMENT.eELEMENT_ID
                        ' return by ID
                        Return WebBrowser1.Document.GetElementById(szElementName)
                    Case ENUM_ELEMENT.eELEMENT_CLASS
                        ' return first by className
                        HTMLcoll = WebBrowser1.Document.GetElementsByTagName("*")
                        'Dim allClasses As IEnumerable(Of HtmlElement) = HTMLcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("ClassName") = tagOrClassName)
                        Dim allClasses As IEnumerable(Of HtmlElement) = HTMLcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("ClassName") = szElementName)
                        ' find index to return by searching allClasses for some identifying attribute = szElementName
                        For Each ele In allClasses
                            If (InStr(ele.OuterHtml, szElementName) <> 0) Then
                                Exit For
                            End If
                        Next
                        Return ele
                    Case ENUM_ELEMENT.eELEMENT_TAG
                        ' return first by tagName
                        'HTMLcoll = WebBrowser1.Document.GetElementsByTagName(tagOrClassName)
                        HTMLcoll = WebBrowser1.Document.GetElementsByTagName(szElementName)
                        ' find index to return by searching HTMcoll for some identifying attribute = szElementName
                        For Each ele In HTMLcoll
                            If (InStr(ele.Name, szElementName) <> 0) Then
                                Exit For
                            End If
                        Next
                        Return ele
                    Case ENUM_ELEMENT.eELEMENT_NAME
                        ' return first by Name
                        HTMLcoll = WebBrowser1.Document.GetElementsByTagName("*")
                        ' this should find only one unique element by name
                        Return HTMLcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("Name") = szElementName).First
                End Select
    
                Application.DoEvents()
    
                GetElement = Nothing
    
                If (GetAbort()) Then
                    HTMLcoll = Nothing
                    GetElement = Nothing
                End If
    
                If (GetDebug()) Then
                    Debug.Print("clsIIE [GetElement Exit]")
                End If
    
            Catch ex As Exception
                Dim szErr As String
                szErr = "Error occured in [GetElement] for the following reason " + ex.Message
                If (GetDebug()) Then
                    Debug.Print(szErr)
                    'gClsExcel.ExcelWriteErrorLog("MenuSelect", szErr)
                End If
                HTMLcoll = Nothing
                GetElement = Nothing
            End Try
    
        End Function
    Why might the single CLASS module not be working? There is a valid HTMLele, so not sure why Nothing is being returned.

    Please confirm I did not screw up the code that was provided somehow. Looks as you provided but does not seem to function now that I am in the process of using the code.

    EDIT: Here is the calling code.

    Code:
        Public Function SelectPIFromList(ByVal szElement As String, ByVal szPERnum As String) As Boolean
    
            Dim HTMLele As HtmlElement
            Dim HTMLeleCol As HtmlElementCollection
            Dim ele As HtmlElement
    
            Dim bStatus As Boolean
    
            Try
    
                If (GetDebug()) Then
                    Debug.Print("clsIIE [SelectPIFromList Enter]")
                End If
    
                If (GetAbort()) Then
                    SelectPIFromList = False
                    Exit Function
                End If
    
                'Assume a success
                bStatus = False
    
                If (IsObjectConnected(WebBrowser1.Document) = False) Then
                    SelectPIFromList = False
                    Exit Function
                End If
    
                bStatus = True
                BrowserWait()
                DoEvnets()
                '                       (nElement,      szElementName,     Optional bGetItem,      Optional nID)
                HTMLele = GetElement(ENUM_ELEMENT.eELEMENT_ID, cID_DIV_VIRTUAL_CONTAINER)
                HTMLele = GetElement(ENUM_ELEMENT.eELEMENT_CLASS, "ColumnValue EvenViewRecord")
    
                'HTMLeleCol = GetElements(ENUM_ELEMENT.eELEMENT_TAG, "tr")
                HTMLeleCol = HTMLele.GetElementsByTagName("tr")
                HTMLele.InvokeMember(HTMLele.InnerHtml)
                Application.DoEvents()
    Last edited by FunkMonkey; May 11th, 2025 at 06:42 PM.

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,093

    Re: Getting CLASS from GetElementsByTagName

    I told... Scope

    Code:
    For Each ele In allClasses
        If (InStr(ele.OuterHtml, szElementName) <> 0) Then
            Exit For
        End If
    Next
    Return ele
    For that to stand any chance of working...

    Code:
    For Each ele In allClasses
        If (InStr(ele.OuterHtml, szElementName) <> 0) Then 
            Return ele
        End If
    Next
    Edit:
    It's probably not OuterHtml
    You're also using szElementName that probably contains the className, as you rejected the tagOrClassName argument i added for the function
    Last edited by .paul.; May 11th, 2025 at 07:40 PM.

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,093

    Re: Getting CLASS from GetElementsByTagName

    Quote Originally Posted by FunkMonkey View Post
    paul. The code provided earlier does not seem to work for returning a single class as I keep getting Nothing. Below is the snapshot of the HTML code I'm working with.

    The Virtual Container
    Name:  Screenshot 2025-05-11 181555.jpg
Views: 670
Size:  9.1 KB


    The "ColumnValue EvenViewRecord". I have tried to use ELEMENT_CLASS and comes up with Nothing. In case the picture does not show up. This class is below the mentioned class with the list of items in the row with an "onclick" for the outerHTML (I think it's outer, hard to remember)

    Name:  Screenshot 2025-05-11 182011.jpg
Views: 569
Size:  14.0 KB
    Code:
        Public Function GetElement(ByVal nElement As Integer, ByVal szElementName As String) As HtmlElement
    
            Dim HTMLcoll As HtmlElementCollection
            Dim ele As HtmlElement
            Dim nCnt As Integer
    
            Try
    
                If (GetDebug()) Then
                    Debug.Print("clsIIE [GetElement Enter]")
                End If
    
                If (GetAbort()) Then
                    GetElement = Nothing
                    Exit Function
                End If
    
                If (IsObjectConnected(WebBrowser1.Document) = False) Then
                    GetElement = Nothing
                    Exit Function
                End If
    
    
                Application.DoEvents()
    
                Select Case nElement
                    Case ENUM_ELEMENT.eELEMENT_ID
                        ' return by ID
                        Return WebBrowser1.Document.GetElementById(szElementName)
                    Case ENUM_ELEMENT.eELEMENT_CLASS
                        ' return first by className
                        HTMLcoll = WebBrowser1.Document.GetElementsByTagName("*")
                        'Dim allClasses As IEnumerable(Of HtmlElement) = HTMLcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("ClassName") = tagOrClassName)
                        Dim allClasses As IEnumerable(Of HtmlElement) = HTMLcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("ClassName") = szElementName)
                        ' find index to return by searching allClasses for some identifying attribute = szElementName
                        For Each ele In allClasses
                            If (InStr(ele.OuterHtml, szElementName) <> 0) Then
                                Exit For
                            End If
                        Next
                        Return ele
                    Case ENUM_ELEMENT.eELEMENT_TAG
                        ' return first by tagName
                        'HTMLcoll = WebBrowser1.Document.GetElementsByTagName(tagOrClassName)
                        HTMLcoll = WebBrowser1.Document.GetElementsByTagName(szElementName)
                        ' find index to return by searching HTMcoll for some identifying attribute = szElementName
                        For Each ele In HTMLcoll
                            If (InStr(ele.Name, szElementName) <> 0) Then
                                Exit For
                            End If
                        Next
                        Return ele
                    Case ENUM_ELEMENT.eELEMENT_NAME
                        ' return first by Name
                        HTMLcoll = WebBrowser1.Document.GetElementsByTagName("*")
                        ' this should find only one unique element by name
                        Return HTMLcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("Name") = szElementName).First
                End Select
    
                Application.DoEvents()
    
                GetElement = Nothing
    
                If (GetAbort()) Then
                    HTMLcoll = Nothing
                    GetElement = Nothing
                End If
    
                If (GetDebug()) Then
                    Debug.Print("clsIIE [GetElement Exit]")
                End If
    
            Catch ex As Exception
                Dim szErr As String
                szErr = "Error occured in [GetElement] for the following reason " + ex.Message
                If (GetDebug()) Then
                    Debug.Print(szErr)
                    'gClsExcel.ExcelWriteErrorLog("MenuSelect", szErr)
                End If
                HTMLcoll = Nothing
                GetElement = Nothing
            End Try
    
        End Function
    Why might the single CLASS module not be working? There is a valid HTMLele, so not sure why Nothing is being returned.

    Please confirm I did not screw up the code that was provided somehow. Looks as you provided but does not seem to function now that I am in the process of using the code.

    EDIT: Here is the calling code.

    Code:
        Public Function SelectPIFromList(ByVal szElement As String, ByVal szPERnum As String) As Boolean
    
            Dim HTMLele As HtmlElement
            Dim HTMLeleCol As HtmlElementCollection
            Dim ele As HtmlElement
    
            Dim bStatus As Boolean
    
            Try
    
                If (GetDebug()) Then
                    Debug.Print("clsIIE [SelectPIFromList Enter]")
                End If
    
                If (GetAbort()) Then
                    SelectPIFromList = False
                    Exit Function
                End If
    
                'Assume a success
                bStatus = False
    
                If (IsObjectConnected(WebBrowser1.Document) = False) Then
                    SelectPIFromList = False
                    Exit Function
                End If
    
                bStatus = True
                BrowserWait()
                DoEvnets()
                '                       (nElement,      szElementName,     Optional bGetItem,      Optional nID)
                HTMLele = GetElement(ENUM_ELEMENT.eELEMENT_ID, cID_DIV_VIRTUAL_CONTAINER)
                HTMLele = GetElement(ENUM_ELEMENT.eELEMENT_CLASS, "ColumnValue EvenViewRecord")
    
                'HTMLeleCol = GetElements(ENUM_ELEMENT.eELEMENT_TAG, "tr")
                HTMLeleCol = HTMLele.GetElementsByTagName("tr")
                HTMLele.InvokeMember(HTMLele.InnerHtml)
                Application.DoEvents()
    That function returns a boolean. Your code is becoming messier and messier. Isolate a function in a tidy environment and iron out the problems one by one.

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    255

    Re: Getting CLASS from GetElementsByTagName

    Quote Originally Posted by .paul. View Post
    That function returns a boolean. Your code is becoming messier and messier. Isolate a function in a tidy environment and iron out the problems one by one.
    No that is correct. I use try/catch for my returns so I know if the function worked properly to carry on. The boolean is always a return of TRUE unless it gets to the CATCH, then the CATCH returns false. Maybe not the best way to do that, but it allows me to make a decission on a pass/fail criteria.

    Code:
                            UpdateRunControls(ENUM_RUN.eRUN_GENERIC_MESSAGE, cMSG_OPENING_PIM, szPInum)
                            If (GetBrowser.SelectPIFromList(cID_DIV_VIRTUAL_CONTAINER, szPInum) = True And GetAbortRun() = False) Then
                                CB_PER_PHASE.SelectedIndex = ENUM_PHASE_LOCATION.ePHASE_MAIN
    
                                GetBrowser.BrowserWait()
                                GetBrowser.DoEvnets()

    This is inside the function. I just did not go all the way down as I was merely showing your code and that it was as copied.

    Here are the results I'm getting from the Class routine you provided. Hope the picture helps?

    Name:  Screenshot 2025-05-11 202043.jpg
Views: 554
Size:  31.7 KB

    EDIT: I did mis-speak. The outerHTML statement was when I was looking up through the 1700 tags, that was a sample as I could not go through 1700. I was using outerHTML as it does have classnames in it for the evaluation for the GetElementsByTagName. Sorry for the confussion.

    Code:
    		InnerHtml	"<tbody><tr><td class=""FieldArea""><span id=""searchScopeMenu"">" & vbLf & vbTab & vbTab & vbTab & vbTab & "<select id=""APPLICATION_PAGE_SEARCH_SCOPE"" onchange=""toggleApplicationPageSearch()"">" & vbLf & vbTab & vbTab & vbTab & vbTab & "<option id=""ViewSearchOption"" value=""View"" selected="""">View" & vbLf & vbTab & vbTab & vbTab & vbTab & "</option>" & vbLf & vbTab & vbTab & vbTab & vbTab & "<option id=""ApplicationSearchOption"" value=""Application"">Application" & vbLf & vbTab & vbTab & vbTab & vbTab & "</option>" & vbLf & vbTab & vbTab & vbTab & vbTab & "</select>&nbsp;" & vbLf & vbTab & vbTab & vbTab & vbTab & "</span><input name=""ADVANCED_VIEW_GLOBAL_SEARCH_COLUMNS"" class=""EmptySearchField"" id=""ADVANCED_VIEW_GLOBAL_SEARCH_COLUMNS"" onkeydown=""handleEnterKey(event, submitViewSearchAllColumnsCommand);stopIEEnterPress(event);stopFirefoxEnterPress(event)"" type=""text"" value=""Search View..."" autocomplete=""off"" emptyvaluetext=""Search View...""></td><td class=""SearchOptionsArea""><a name=""SaveSearchLink"" title=""Save Search"" class=""pointer SaveSearchIconClass"" id=""SaveSearchLink"" href=""javascript:openSaveSearchDialog('SaveSearchLink')""></a><a name=""LoadSearchLink"" title=""Load Search"" class=""pointer LoadSearchIconClass"" id=""LoadSearchLink"" href=""javascript:openLoadSearchDialog('LoadSearchLink')""..."	String
    Code:
    		OuterHtml	"<table class=""IconField ViewSearchField"" cellspacing=""0"" cellpadding=""0""><tbody><tr><td class=""FieldArea""><span id=""searchScopeMenu"">" & vbLf & vbTab & vbTab & vbTab & vbTab & "<select id=""APPLICATION_PAGE_SEARCH_SCOPE"" onchange=""toggleApplicationPageSearch()"">" & vbLf & vbTab & vbTab & vbTab & vbTab & "<option id=""ViewSearchOption"" value=""View"" selected="""">View" & vbLf & vbTab & vbTab & vbTab & vbTab & "</option>" & vbLf & vbTab & vbTab & vbTab & vbTab & "<option id=""ApplicationSearchOption"" value=""Application"">Application" & vbLf & vbTab & vbTab & vbTab & vbTab & "</option>" & vbLf & vbTab & vbTab & vbTab & vbTab & "</select>&nbsp;" & vbLf & vbTab & vbTab & vbTab & vbTab & "</span><input name=""ADVANCED_VIEW_GLOBAL_SEARCH_COLUMNS"" class=""EmptySearchField"" id=""ADVANCED_VIEW_GLOBAL_SEARCH_COLUMNS"" onkeydown=""handleEnterKey(event, submitViewSearchAllColumnsCommand);stopIEEnterPress(event);stopFirefoxEnterPress(event)"" type=""text"" value=""Search View..."" autocomplete=""off"" emptyvaluetext=""Search View...""></td><td class=""SearchOptionsArea""><a name=""SaveSearchLink"" title=""Save Search"" class=""pointer SaveSearchIconClass"" id=""SaveSearchLink"" href=""javascript:openSaveSearchDialog('SaveSearchLink')""></a><a name=""LoadSearchLink"" title=""Load Search"" class=""pointer LoadSearchIconClass"" id=..."	String
    Last edited by FunkMonkey; May 11th, 2025 at 08:52 PM.

  20. #20
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,093

    Re: Getting CLASS from GetElementsByTagName

    What is the class name? What is the identifying text you’re searching for in the tags?
    You need separate arguments in your method signature as the text you’re searching for shouldn’t be the same as the className

  21. #21
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,093

    Re: Getting CLASS from GetElementsByTagName

    If you refine the elements to just the elements with the correct className, you can see you need a unique string to search for in the elements returned. It looks like allclasses contains nothing…

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    255

    Re: Getting CLASS from GetElementsByTagName

    Quote Originally Posted by .paul. View Post
    What is the class name? What is the identifying text you’re searching for in the tags?
    You need separate arguments in your method signature as the text you’re searching for shouldn’t be the same as the className
    The class is below.
    The bolded item below is where I start. I get the ID for this section and then using that HTMLele I use that to get the CLASS for the below code.

    Code:
    <div id="div_virtual_container" style="top: 276px; width: 1171px; height: 148px; overflow: hidden; position: fixed;">
                                                <div id="SelectAllCode" style="display: none;"></div>
                                                    
    
    
    
    
    <table class="TableColumnTitle" style="width: 1182px;" cellspacing="0" cellpadding="0">
        <tbody>
        
        
        
    <tr title="" class="ColumnValue  EvenViewRecord SelectedViewRecord" onclick="javascript: var win = openTabByDocID('2030137', 'true',event);" data-islastelement="true">                                                                
            <td width="38" style="padding: 0px; width: 38px;"><input name="ETQ$SELECTED_DOCUMENTS" type="checkbox" value="ETQ$APPLICATION_NAME=COMPLAINTS&amp;ETQ$FORM_NAME=SJM_COMPLAINTS_PRODUCT_INVESTIGATION_P&amp;ETQ$KEY_NAME=S_COMP_P_INVESTIG_ID&amp;ETQ$KEY_VALUE=2030137"><span class="ViewDocumentLinks"><a tabindex="-1" class="ViewDocumentLink" href="javascript: var win = openTabByDocID('2030137', 'true',event);"></a></span></td>
            <td width="200" style="width: 200px; text-indent: 8px;">PI-2023-0138285-01</td>
    	<td width="103" style="width: 103px; text-indent: 8px;">Main Investigation</td>
    	<td width="200" style="width: 200px; text-indent: 8px;">Epperson, Brad</td>
    	<td width="129" style="width: 130px; text-indent: 8px;"><div class="DateColumn">Apr 29, 2025</div></td>
    	<td width="130" style="width: 130px; text-indent: 8px;"><div class="DateColumn">Nov 18, 2024</div></td>
    	<td width="250" style="width: 250px; text-indent: 8px;">Neuromodulation Division</td>
    	<td width="117" class="EmptyColumn" id="ETQ$EMPTY_COLUMN" style="width: 118px; text-indent: 8px;">&nbsp;</td>
    	
        <td width="30" style="width: 30px; text-indent: 8px;">&nbsp;</td></tr></tbody></table>
    
                                            </div>
    What I'm looking for is in the picture below. There could be as many as *-01 to *-10, or more numbers highlighted above. So I do a For Each loop to check to grab the correct requested number of the record.

    Name:  Screenshot 2025-05-12 180935.jpg
Views: 459
Size:  17.7 KB

    essentially I'm using this as the verification I have the correct record before I perform a ele.InvokeMember("click") to select that record. I do more later to grab the header information for setting up the GUI, but I don't seem to have an issue with this area, but getting the InvokeMember to fire on the "onclick" event for the above HTML code seems to be the hiccup with the correct element, which is usually Nothing currently. However, if I do a ID for the section that works just fine, but the CLASS seems to be stubborn.

    Both of the functions you provided return "Nothing" for either the single element, or the group element (which I will need when there are multiple records of the same batch).

    Hope this makes more sense?

    EDIT: I have even hard coded the class name into the GetElements -> CLASS but that did not help either. It still returned Nothing.
    Last edited by FunkMonkey; May 12th, 2025 at 06:27 PM.

  23. #23
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,093

    Re: Getting CLASS from GetElementsByTagName

    I think it might be due to the nesting of elements. But, if you have found a way to get the group of elements by ID, you could use LINQ to get the element from that group of elements with LINQ using the className

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    255

    Re: Getting CLASS from GetElementsByTagName

    Quote Originally Posted by .paul. View Post
    I think it might be due to the nesting of elements. But, if you have found a way to get the group of elements by ID, you could use LINQ to get the element from that group of elements with LINQ using the className
    It seems that for certain functions, in order to get the class items I need, I will have to go back to the Object way. Can't seem to get a class with the current way and need a class to get the items I need from a For Each loop.

    It was fun while it lasted, and thanks for the help. Perhaps if I get to know LINQ I might be able to do something but right now it is beyond my ability.

    Thanks to everyone who offered input.

  25. #25
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,093

    Re: [RESOLVED] Getting CLASS from GetElementsByTagName

    The LINQ we showed you doesn't need much understanding. It was complete. Just needed tweaking in the searchFor part...

    Code:
    HTMLcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("Name") = szElementName).First
    Imagine having an array of HtmlElement...

    Code:
    Dim elements() As HtmlElement = {element1, element2, element3}
    This is basically HTMLcoll, after .Cast(Of HtmlElement)

    Code:
    Dim uniqueElement As HtmlElement = elements.Where(Function(el As HtmlElement) el.GetAttribute("someAttribute") = someVariable).First
    This is just a compact way of looping through the array, does exactly the same as...

    Code:
    Dim uniqueElement As HtmlElement
    For each el As HtmlElement in elements
        If el.GetAttribute("someAttribute") = someVariable Then
            uniqueElement = el
            Exit For
        End If
    Next

  26. #26
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,093

    Re: [RESOLVED] Getting CLASS from GetElementsByTagName

    At least use the basic array searching with a loop. Using Late Binding is a bad choice, which will impact you when you ask for help...

  27. #27

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    255

    Re: [RESOLVED] Getting CLASS from GetElementsByTagName

    Quote Originally Posted by .paul. View Post
    The LINQ we showed you doesn't need much understanding. It was complete. Just needed tweaking in the searchFor part...

    Code:
    HTMLcoll.Cast(Of HtmlElement).Where(Function(el As HtmlElement) el.GetAttribute("Name") = szElementName).First
    Imagine having an array of HtmlElement...

    Code:
    Dim elements() As HtmlElement = {element1, element2, element3}
    This is basically HTMLcoll, after .Cast(Of HtmlElement)

    Code:
    Dim uniqueElement As HtmlElement = elements.Where(Function(el As HtmlElement) el.GetAttribute("someAttribute") = someVariable).First
    This is just a compact way of looping through the array, does exactly the same as...

    Code:
    Dim uniqueElement As HtmlElement
    For each el As HtmlElement in elements
        If el.GetAttribute("someAttribute") = someVariable Then
            uniqueElement = el
            Exit For
        End If
    Next
    The code is not returning a class at all, it's always Nothing. I hardcoded a class into where the variable was, and I know that class was valid, yet all I got was Nothing. It's not just a matter of looping. I first need to have a valid Element before I can do any looping for what I'm looking for.

    I understand about the looping, as my old VBA code does much of that as well but I am not sure what to debug with the code as it is not simple as stated. I looked through examples and yes some are fairly straight forward, but I'm lost at debugging the code you provided. I may try to see if I can find a simple tag to class example, but I'm not going to hold my breath. I'll post what I find.

  28. #28

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    255

    Re: [RESOLVED] Getting CLASS from GetElementsByTagName

    Quote Originally Posted by .paul. View Post
    At least use the basic array searching with a loop. Using Late Binding is a bad choice, which will impact you when you ask for help...
    I understand. See my post before this one.

  29. #29
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,093

    Re: [RESOLVED] Getting CLASS from GetElementsByTagName

    You should get the tr element like this.

    Code:
    HTMLeleCol = WebBrowser1.Document.GetElementsByTagName("tr")
    Dim allClasses As New List(Of HtmlElement)
    For each el As HtmlElement in HTMLeleCol 
        If el.GetAttribute("className") = "ColumnValue  EvenViewRecord SelectedViewRecord" Then
            allClasses.Add(el)
            Exit For
        End If
    Next
    The child elements are td.

    HTMLeleCol = WebBrowser1.Document.GetElementsByTagName("td")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width