Assuming all of the elements are siblings, as shown in your question, then you can use the
:last-of-type[
^] selector to find the last
<br>
element, and then iterate over its following siblings.
For example:
const doc = new DOMParser().parseFromString(file, "text/html");
const lastBr = doc.querySelector("br:last-of-type");
const elementsToProcess = [];
let el = lastBr ? lastBr.nextElementSibling : doc.firstChild;
while (el) {
if (el.tagName === "P") { elementsToProcess.push(el); }
el = el.nextElementSibling;
}
Demo[
^]