最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Puppeteer How to check if the page has redirected - Stack Overflow

programmeradmin1浏览0评论

I am trying to check with waitForNavigation() to see if the page switches or not. Here is the code I am using

if ((await page.waitForNavigation()) == null) {
  console.log("False");
  await browser.close();
} else {
  console.log("True");
  await browser.close();
}

For example, let us just say a box on the screen is there and you get redirected to another page. I want True or False to pop up on the console.log.

I had to switch up the answer a bit but here it is

if (newUrl == startingURL) {
  console.log("False");
  await browser.close();
} else {
  console.log("True");
  await browser.close();
}

I am trying to check with waitForNavigation() to see if the page switches or not. Here is the code I am using

if ((await page.waitForNavigation()) == null) {
  console.log("False");
  await browser.close();
} else {
  console.log("True");
  await browser.close();
}

For example, let us just say a box on the screen is there and you get redirected to another page. I want True or False to pop up on the console.log.

I had to switch up the answer a bit but here it is

if (newUrl == startingURL) {
  console.log("False");
  await browser.close();
} else {
  console.log("True");
  await browser.close();
}
Share Improve this question edited Apr 29, 2023 at 10:01 Manas Khandelwal 3,9462 gold badges14 silver badges25 bronze badges asked Apr 21, 2020 at 18:48 Neik0Neik0 1033 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

If you only care about redirects, like HTTP redirects & window.location.href redirects, you can check the URL that you end up on and pare it to the URL you started with:

const startURL = 'https://example./redirect-start'
await page.goto(startURL)
if (page.url() === startURL) { console.log('False') }
else { console.log('True') }
发布评论

评论列表(0)

  1. 暂无评论