Search code examples
vb.netxpathhtml-agility-packsiblings

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


How to create xpath for the following sibling elements of "a" and "span" which i need to find href link for the span class elements of "METADEC Co.Ltd."

Locations of the elements are in the same level

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

I need to get the href value which span text value is matching

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

full web page link;

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

Finally, I have reached to my target with the help of answers as shown below ;

        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

Thanks for the help.


Solution

  • In "Participant’s contact METADEC Co.Ltd. ( BENDIG )" you want to get link from "BENDIG" when not having that (a) element in your collection?

    There are multiple ways, shortest would be:

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

    or

    Item's next sibling

    Both one line or none, however you count it.