hi. i wrote a simple script thas is supposed to acomplish the following task:
- open a bowser in a url that was recevied in the command line
- wait until the value of the address bar changes
- write the new address bar value to a file
- close the browser.
i keep getting chrome-error://chromewebdata/ instead of the actual value (which is supposed to be localhost with some code that is generated, and nothing presented on screen.)
anyone know why is this behevior is happning?
code added for reference.
const puppeteer = require('puppeteer');
const fs = require('fs');
(async () => {
// Get the initial URL from the command line arguments
const initialUrl = process.argv[2];
if (!initialUrl) {
console.error('Usage: node script.js <URL>');
process.exit(1);
}
// Launch the browser
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
// Go to the initial URL
await page.goto(initialUrl, {waitUntil: 'networkidle2'});
// Listen for the 'framenavigated' event
page.on('framenavigated', frame => {
if (frame === page.mainFrame()) {
const newUrl = frame.url();
if (newUrl !== initialUrl) {
// Write the new URL to a file and close the browser
fs.writeFileSync('newUrl.txt', newUrl);
console.log(`New URL (${newUrl}) saved to newUrl.txt`);
browser.close();
}
}
});
})();