c#, HtmlAgilityPack: Can someone please explain why this xpath grabs top of page and not a child element?

moondaddy 856 Reputation points
2021-02-18T16:40:57.453+00:00

Using the attached webpage I'm trying to grab the text highlighted i dark blue below.
However, because I will also be grabbing all the other data below it, I am first grabbing the element highlighted in yellow as the starting point which is:

69693-image.png

//h3 [text()='Business Details']/../following-sibling::div  

If I add this to the above xpath I can get the text I want:

/div[1]/div/div [text()]   

69771-image.png

However, if I do this in 2 steps in c# the same path grabs something at the top of the page. Here's my code

    static void BusDetailsTest()  
    {  
        HtmlDocument doc = new HtmlDocument();  
        doc.Load(@"D:\Apps\VSOCD\LeadaRator\Trunk\V01\WpfBrowserTest\SampleSourceFiles\5-5.2 Number19.txt");  
        // Get the outer element that can be used as a starting point for all the other pieces of data to scape also.  
        var h3 = doc.DocumentNode.SelectSingleNode("//h3 [text()='Business Details']/../following-sibling::div");  
        // Why does this grab something at the top of the page rather than some nexted child divs?  
        var divBusDesc = h3.SelectSingleNode("//div[1]/div/div");  
        Console.WriteLine(divBusDesc.InnerText);  
    }  

Can someone please explain what I'm doing wrong and how to correct it?

Thank you

I tried to upload a text file with the page source but this site is real buggy today and wont allow attaching text files.

69777-image.png

69747-image.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,007 questions
{count} votes