0

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.

Ali
  • 33
  • 9
  • 2
    Possible duplicate of [How to select following sibling/xml tag using xpath](https://stackoverflow.com/questions/3139402/how-to-select-following-sibling-xml-tag-using-xpath) – Andrew Morton Oct 13 '18 at 18:44
  • I suggested one previous question which this might be a duplicate of, but if you look on this page over to the right you will find a section "Related" which might have better anwers for this specific problem. – Andrew Morton Oct 13 '18 at 18:46
  • @Andrew , I have review it and dont think that same ( might be because of my experience level). My question was looking for a href attributes of “a” which is a following sibling of span and text value of matching with my criteria – Ali Oct 13 '18 at 19:01
  • You didn't include url of website in your question, and the website broke. – CruleD Oct 13 '18 at 19:02
  • @CruleD added now which you are familiar with it before – Ali Oct 13 '18 at 19:07
  • @Ali The idea is to get the following sibling. When you have that, you can get the value of the href attribute of that following sibling (or any other attribute if needed). – Andrew Morton Oct 13 '18 at 19:32
  • @AndrewMorton this is what i have but still not working, any idea? //span[@class='texte' and contains(normalize-space(text()),'" & TxtParticipantName.Text & "')]/following-sibling::a[@class='contenu']/@href" – Ali Oct 13 '18 at 22:08

1 Answers1

1

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.

CruleD
  • 1,153
  • 2
  • 7
  • 15