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

javascript - Puppeteer wait for url - Stack Overflow

programmeradmin2浏览0评论

I have headless off and I want to wait untill user redirect to some page. I could use waitForRequest from puppeteer API but I don't know exact url it just must pass few circumstances.

So I use waitForFunction and check circumstances there but when I redirect to correct URL then I need to refresh page to pass circumstances for some reason.

My code is:

try {
    await page.waitForFunction(() => {
        if(window &&
           window.location &&
           window.location.hostname) {
            const host = window.location.hostname.split('.');
            if(!host.includes('www') &&
               !host.includes('new') &&
               host.includes('margonem') &&
               host.length === 3) {
                return true;
            }
        }
    }, {
        polling: 200,
        timeout: 0
    })
} catch (e) {
    console.log(e);
}

and when I redirect to URL which pass all of above if's then I need to reload page to actually see that it return true. Why it works like this? I don't want user to be forced to refresh page after he enter correct one.

I have headless off and I want to wait untill user redirect to some page. I could use waitForRequest from puppeteer API but I don't know exact url it just must pass few circumstances.

So I use waitForFunction and check circumstances there but when I redirect to correct URL then I need to refresh page to pass circumstances for some reason.

My code is:

try {
    await page.waitForFunction(() => {
        if(window &&
           window.location &&
           window.location.hostname) {
            const host = window.location.hostname.split('.');
            if(!host.includes('www') &&
               !host.includes('new') &&
               host.includes('margonem') &&
               host.length === 3) {
                return true;
            }
        }
    }, {
        polling: 200,
        timeout: 0
    })
} catch (e) {
    console.log(e);
}

and when I redirect to URL which pass all of above if's then I need to reload page to actually see that it return true. Why it works like this? I don't want user to be forced to refresh page after he enter correct one.

Share Improve this question asked Oct 18, 2019 at 12:34 BT101BT101 3,82611 gold badges48 silver badges101 bronze badges 1
  • This is an old post that became canonical, but it's missing some context which would make it more useful for new visitors. For example, is there an element on the page you eventually land on that you begin manipulating on arrival? If so, wait on that selector rather than the navigation. waitForFunction() should work as well, and generally avoid waitForNavigation() except as a last resort. Avoid timeout: 0 since it'll cause your script to hang forever, so it'll never throw and log and you'll have a hard time debugging it since you'll be forced to kill the process. Who is the "user" here? – ggorlen Commented Nov 20, 2024 at 22:57
Add a comment  | 

4 Answers 4

Reset to default 8

The simplest way I have found is using waitForFunction, it is simple to modify to fit your specifications as well as compact.

  await page.waitForFunction("window.location.pathname == '/Welcome.aspx'")

I have headless off and I want to wait untill user redirect to some page.

just use waitForNavigation().

In combination with a click make sure you use this pattern:

const [response] = await Promise.all([
  page.waitForNavigation(waitOptions),
  page.click(selector, clickOptions),
]);

waitForNavigation also returns a response object that you can then inspect

I could use waitForRequest from puppeteer API but I don't know exact url it just must pass few circumstances.

in this case puppeteer injects the request as argument and you can just test this in your lambda function. For example:

page.waitForRequest(request => {
  return  request.url().includes('margonem') && request.method() === 'GET'
})

Sometimes we need to wait until we reach a specific URL.

The best way to handle it is

    let holdProgress = true;
        while (holdProgress) {
            await page.waitFor(300);
            if (page.url().includes('/env-selector')) {
                holdProgress = false;
            }
        }

In case anyone wants to ignore unwanted navigations until it matches your desired url. This one could help:

while (true) {
  try {
    await page.waitForNavigation({
      timeout: 0,
    });

    await page.waitForFunction("window.location.pathname == 'blank'");

    break;
  } catch (error) {
    continue;
  }
}
发布评论

评论列表(0)

  1. 暂无评论