Trying to run custom JS in Puppeteer using a clean context untouched by page modifications. How can I reliably perform DOM queries? Check this sample:
const browserTool = require('puppeteer');
(async () => {
const instance = await browserTool.launch({ headless: false });
const tab = await instance.newPage();
await tab.goto('file:///path/to/example.html', { waitUntil: 'networkidle0' });
const output = await tab.evaluate(() => {
const item = document.getElementById('sample');
return item ? item.innerText : 'No Data';
});
console.log(output === 'Test' ? 'Success!' : 'Failure!');
})();
i’ve had luck with evaluateOnNewDocumnt to inject your js before any page mods. it works fine for keepin an unmodified enviroment. give it a shot!
In my experience, using page.evaluateOnNewDocument to inject code before any other page scripts execute has proven to be a reliable method. This technique ensures that your custom JavaScript runs in a pristine context, allowing for accurate DOM queries without external interference. I have also experimented with creating isolated incognito browser contexts for added isolation, which can sometimes offer more predictability in complex environments. Adjusting the point of injection and testing with smaller, focused snippets has been key to fine-tuning the approach for different applications.
I’ve encountered similar challenges when trying to execute custom JavaScript in an unmodified Puppeteer context. In my experience, relying solely on evaluateOnNewDocument can sometimes be insufficient if other initializations are in play. To mitigate this, I inject a minimal bootstrap script that overrides or preserves certain global objects, ensuring that when my main script runs, the environment is as pristine as possible. Using isolated contexts like incognito sessions further guarantees that no residual state interferes with DOM querying. Careful timing and testing with various sample pages has been key to refining this process.
i’d try using a fresh incognito context or delay js inject slightly after page load. sometimes evaluateOnNewDocument runs too early and gets overridden by page scipts. fiddlin with the timing has helped me get a cleaner environment. good luck!