0

如何从最后带有令牌或数字 ID 的元素中获取完整 ID,类似这样的东西,但 VB.NET 似乎并没有以相同的方式处理 querySelector。

基于部分字符串的Javascript getElementById

例如。

4

1 回答 1

0

适合您的解决方案是:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim wb1 As WebBrowser = New WebBrowser
        Dim resultElements As List(Of HtmlElement) = New List(Of HtmlElement)

        Dim processElement As Action(Of HtmlElement) = Sub(element As HtmlElement)

                                                           Dim elementID As String = element.GetAttribute("id")


                                                           If Len(elementID) > 0 Then
                                                               Console.WriteLine(elementID)
                                                           End If

                                                           'You can change this with your conditions
                                                           If elementID IsNot Nothing AndAlso
                                                           elementID.EndsWith("_01") AndAlso
                                                           resultElements.IndexOf(element) = -1 Then

                                                               resultElements.Add(element)

                                                           End If

                                                           'Or put here other conditions
                                                           'If elementName IsNot Nothing AndAlso elementName.StartsWith("01_ABCD") AndAlso resultElements.IndexOf(element) = -1 Then resultElements.Add(element)


                                                           If element.Children IsNot Nothing AndAlso element.Children.Count > 0 Then

                                                               For Each children In element.Children
                                                                   processElement(children)
                                                               Next

                                                           End If


                                                       End Sub


        AddHandler wb1.DocumentCompleted, Sub(senderWb As Object, eWb As WebBrowserDocumentCompletedEventArgs)

                                              Dim allElements = wb1.Document.All

                                              If allElements IsNot Nothing And allElements.Count > 0 Then
                                                  For Each element As HtmlElement In allElements
                                                      processElement(element)
                                                  Next
                                              End If

                                              ' Here You have all your elements with custom ID condition 
                                              If resultElements.Count > 0 Then

                                                  For Each okElement As HtmlElement In resultElements
                                                      Console.WriteLine(okElement.GetAttribute("id"))
                                                  Next

                                              End If

                                          End Sub

        wb1.Navigate("http://stackoverflow.com")

    End Sub

通知我:)

于 2019-10-24T08:19:27.657 回答