0

I try to extract the word "Headline2" with XPath from a nested "div" container. The problem is that there are no explicit class names to use as an identifier.

Therefore I've tried to select the 4th div like that- without a certain class identifier, but I don't get a result.

$xpath->query('//div[contains(@id, "EU-group-keep")]//div//div//div//div');

What is the correct way to extract the 4th div container without the use of a certain class identifier?

<div class="tiw-group" id="EU-group-keep" data-type="multi-line">
        <div class="tiw-group-ending"></div>
        <div class="tiw-group-quantity"></div>
        <div class="tiw-group-name"><a href="test">[Headline1]</a></div>
            <div class="tiw-line-ending">Headline2</div>
            <div class="tiw-line-quantity"></div>
            <div class="tiw-line-name" id="group-keep-line-0"><span></span></div>
        </div>
</div>
Zerers
  • 63
  • 6
  • HTML fragment contains errors: `tmp.html:9: HTML parser error : Unexpected end tag : div`. Which would be the 4th div for you? – LMC Dec 15 '22 at 22:44

1 Answers1

0

Assuming this is the HTML sample (with readable format)

<div class="tiw-group" id="EU-group-keep" data-type="multi-line">
        <div class="tiw-group-ending"></div>
        <div class="tiw-group-quantity"></div>
        <div class="tiw-group-name"><a href="test">[Headline1]</a></div>
        <div class="tiw-line-ending">Headline2</div>
        <div class="tiw-line-quantity"></div>
        <div class="tiw-line-name" id="group-keep-line-0"><span></span></div>
<!-- ending tag not needed here? -->
<!-- </div> -->
</div>

This xpath gets the expected element

 //div[@id="EU-group-keep"]//div[4]
LMC
  • 10,453
  • 2
  • 27
  • 52