Kex
Kex

Reputation: 8609

Listening for URL changes in puppeteer

Is there a way to listen for URL changes/redirects in Puppeteer? For example if I point the browser to http://www.somesite.com/home and it redirects to http://www.somesite.com/login, I would like a way to observe these changes and then terminate the current action.

I tried this:

await page.setRequestInterception(true);
page.on('request', interceptedRequest => {
    console.log(interceptedRequest.url());
    interceptedRequest.continue();
});

but this is just all the requests. It doesn't log when the url changes.

Upvotes: 7

Views: 3773

Answers (1)

Ofer Segev
Ofer Segev

Reputation: 5282

How about

page.on("framenavigated", frame => {
    const url = frame.url(); // the new url
    
    // do something here...
});

Upvotes: 10

Related Questions