Puppeteer: How Can I Retrieve a Parent Container?

I need Puppeteer to locate a container by its “1st Goal” text, obtain its parent, and click a specific nested element (score 200).

<div class="containerSet">
  <div class="headerTitle"><span>1st Goal</span></div>
  <div class="detailsSection">
    <div class="infoArea">
      <div>
        <div><span>Team Alpha</span><span class="result">200</span></div>
        <div><span>No Goal</span><span class="result">300</span></div>
        <div><span>Team Beta</span><span class="altResult">400</span></div>
      </div>
    </div>
  </div>
  <div></div>
</div>

hey, try using page.$x() to find the span with ‘1st goal’ then climb up via parentElement until you hit the container. Once there, locate and click that nested element. hope this helps, not 100% but worked for me sometimes

In my experience it is useful to combine DOM methods with Puppeteer’s built-in evaluation tools. I found that using page.evaluate() to locate a container that contains the desired text is more reliable than working only with XPath. Once the element containing ‘1st Goal’ is found, script a function to traverse upward until the container is found. Then, within that container, search for the child element displaying ‘score 200’ and perform the click() method on it. This approach has proven both precise and consistent in my projects.

I encountered similar challenges before and found that combining querySelector with page.evaluate often gives better control over DOM manipulation in Puppeteer. I usually locate the text node by directly querying for the span with ‘1st Goal’, then use a while loop in the evaluation to traverse up to the container element. After identifying the proper parent, I search for the deeply nested element representing the score and then trigger the click event. This method has proven reliable in multipage setups where elements dynamically load, reducing dependency on a single-step query and making my scripts more robust.

hey i used page.evaluate to grab the span and then trasversed up until i hit the container. from there i got the nested score element and clicked it. works well on my setup, hope it works for u too