Solved: How do you select this text above the HTML table using XPath?


Below is the HTML I’m working with. I’ve removed some lines that aren’t relevant to this question, such as the content within the table.
My objective is to capture the names, and the corresponding information found within the table. Each Name/Table combination would be one row.
I currently have this XPath '//div[@class="row"]/div/span[@class="LIST_TITLE"][contains(text(),"Contact Person")]/ancestor::div/div[@class="col-sm-6-left"]/table', which I am able to loop over to extract out the information in the table.
My issue is how to capture the name for each table, which I am finding difficult as they’re both contained within the same tag.
I have tried using './ancestor::div[1]/text()', though this will capture both names.
Any help is greatly appreciated

Best Answer:

preceding-sibling::text()[1] will return the text node prior to the context node. If the table elements are used as the context node, that will return you the following text nodes:
NB I don’t know what web scraping tool you are using, but I know that some of them have XPath APIs that won’t return text nodes; only elements. If that’s the case for you, you might need to switch to a different XPath API that is capable of returning text nodes, e.g. lxml

If you have better answer, please add a comment about this, thank you!