I’m working on a project where I need to collect clickable items from a webpage and simulate clicks on them to track redirections and capture the final destination URLs. My current approach works but I’m missing some network traffic that happens before I can set up the request listeners.
const newTab = await browserInstance.newPage();
const targetUrl = "example-website-url";
await newTab.goto(targetUrl, {
waitUntil: 'networkidle2',
timeout: 25000
});
// Find clickable items on page
let linkElements, clickButtons, allClickables;
linkElements = await newTab.$$('a[href]');
clickButtons = await newTab.$$('input[type="submit"], button');
allClickables = linkElements.concat(clickButtons);
// Process each clickable item
for (let index = allClickables.length - 1; index >= 0; index--) {
// Listen for new tab creation
const tabCreationPromise = new Promise(resolve =>
browserInstance.once('targetcreated', target => resolve(target.page()))
);
allClickables[index].click({button: 'middle'});
// Get the newly created tab
const createdTab = await tabCreationPromise;
if(createdTab) {
createdTab.on('request', networkRequest => {
console.log(`Captured URL: ${networkRequest.url}`);
});
}
}
The problem is that initial redirects occur before I can attach the request listener using the page.on('request')
method. Is there a way to set up global network monitoring for all new pages created by the browser instance? I need to catch all network activity from the very beginning.