如何为具有匹配条件的以下兄弟姐妹创建 xpath

How to create a xpath for following siblings which has a matching criteria

如何为 "a" 和 "span" 的以下兄弟元素创建 xpath,我需要为 [=32] 的跨度 class 元素找到 href link =]

元素位置在同一层级

/html/body/table/tbody/tr/td[2]/table[4]/tbody/tr[2]/td[2]/

<span class="texte">METADEC Co.Ltd.</span>
<a href="www.something.com" class="contenu"> BENDIG </a>

我需要获取 span 文本值匹配的 href 值

Dim ParticipantNodes As HtmlNodeCollection = doc.DocumentNode.SelectNodes("/html/body/table/tr/td[2]/table[4]/tr[2]/td[2]//span[@class='texte']")
    For Each item As HtmlNode In ParticipantNodes
        If item.Name = "span" And item.InnerText <> "" Then
            If item.InnerText.Contains("METADEC Co.Ltd.") Then
                result = item.Attributes("href").Value 
                Exit For
            End If
        End If
    Next

完整网页 link;

http://www.eurovent-certification.com/en/Certified_products/Access_by_programme.php?lg=en&rub=04&srub=01&select_prog=AHU

终于,在答案的帮助下,我达到了我的目标,如下所示;

        Dim inputOk As Boolean = False
        Dim n As Integer = 1
        Do Until inputOk = True
            Try
                Dim dr As HtmlNode = ParticipantNodes.SelectSingleNode(".//span[@class='texte' and contains(normalize-space(text()),'" & TxtParticipantName.Text & "')]/following-sibling::a[" & n & "]")
                If Trim(dr.InnerText.Replace(vbLf, "").Replace(vbCr, "").Replace(vbTab, "").Replace("&nbsp;", "")) = TxtBrand.Text Then
                    templink = "http://www.eurovent-certification.com/en/Certified_products/Access_by_programme.php" & dr.Attributes("href").Value & "MB+%2F+MB+%2F+MECH"
                    inputOk = True
                End If
            Catch ex As Exception
                MsgBox(ex.ToString)

            End Try
            n += 1
        Loop

感谢您的帮助。

在 "Participant’s contact METADEC Co.Ltd. ( BENDIG )" 中,当您的集合中没有该 (a) 元素时,您想从 "BENDIG" 中获取 link?

有多种方式,最短的是:

Get Item's parent then the next item (child of that parent)

Item's next sibling

无论是一行还是none,随便你怎么算。