Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer get URL from localhost

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.

connection refused

like image 383
Luke_KS Avatar asked Dec 18 '25 16:12

Luke_KS


1 Answers

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.

like image 154
Arsinclair Avatar answered Dec 21 '25 07:12

Arsinclair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!