title: How to use XPath in a newly opened window?
body: <p>With the code below (intended for a bookmarklet), I am trying to open a new window, look for certain span-elements, and click each of them. However, I cannot access the code of the new window through XPath.</p>
  <ul>
  <li>Inserting the code (copy &amp; paste) of the <code>clickElem function</code> directly in the new tab works fine</li>
  <li>CORS is not a problem since it's the same domain/subdomain/protocol</li>
  <li>I've been also following <a href="https://stackoverflow.com/questions/51310680/how-can-i-run-a-script-in-another-newly-opened-tab">this answer</a>.</li>
  </ul>
  <p><strong>JavaScript:</strong></p>
  <pre class="lang-js prettyprint-override"><code>const w = window.open('https://example.com', 'Example', 'width=500, height=500');
  w.clickElem = () =&gt; {
      const xpath = '//span[text()=&quot;Click here&quot;]';
      const selectedNodeElements = w.document.evaluate(xpath, document, null, XPathResult.ANY_TYPE, null);
      let currentNode = selectedNodeElements.iterateNext();
      while (currentNode) {
          currentNode.click();
          currentNode = selectedNodeElements.iterateNext();
      }
  }
  setTimeout(w.clickElem, 8000);
  </code></pre>
  <p>When I try to access the text via <code>currentNode.textContent</code> I receive following error:</p>
  <p><code>&quot;Error in protected function: Cannot read properties of null (reading 'textContent')&quot;</code></p>
  <p>Grateful for every hint!</p>