I'm writing a parser that should extract "Extract This Text" from the following html:
<div class="a">
<h1>some random text</h1>
<div class="clear"></div>
Extract This Text
<p></p>
<h2></h2>
</div>
I've tried to use:
document.querySelector('div.a > :nth-child(3)');
And even by using next sibling:
document.querySelector('div.a > :nth-child(2) + *');
But they both skips it and returns only the "p" element.
The only solution I see here is selecting the previous node and then using nextSibling
to access it.
Can querySelector
select text nodes at all?
Text node: https://developer.mozilla.org/en-US/docs/Web/API/Text
querySelector
to select the element and then extract the#text
node withArray.from(element.childNodes).find(node => node.nodeName === '#text')
– Zaidazailerelement.childNodes[2].textContent
– Peloquin