I have a website where I need to log in. After a short time of loading, Chrome shows the "connection refused" page. At that point, I need to get the URL.
I tried multiple approaches I found online, however, none of them worked in that case.
What I tried
const url = await page.url();
console.log(url) //--> chrome-error://chromewebdata/
const url = await page.evaluate(() => document.location.href);
console.log(url); //--> chrome-error://chromewebdata/
I also think it has nothing to do with the timing. Because if I try to use puppeteer to click the "Details" Butten everything works well, so I assume the page is loaded propperly at that point.

You could use a Chrome Devtools Protocol session to capture the requests and responses.
const client = await page.target().createCDPSession();
await client.send('Network.enable');
client.on('Network.requestWillBeSent', (e) => {
if (e.request.url.includes("http://localhost/dead-url")) {
console.log(e.request.url);
}
});
While doing that you could also check for e.redirectResponse, but it's going to be there only for a successful redirect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With