如何在Playwright中等待特定脚本在页面上加载?

huangapple go评论141阅读模式
英文:

How to wait for a specific script to load on page in Playwright?

问题

在Playwright中,您可以使用以下方式实现相同的功能:

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();

  // 使用page.route来拦截网络请求
  await page.route('**/script.js', (route) => {
    route.continue(); // 允许请求继续
  });

  await page.goto('https://hello.com/');

  // 等待网络请求完成
  await page.waitForRequest('**/script.js', { timeout: 20000 });

  await browser.close();
})();

请注意,Playwright中的网络请求拦截与Cypress的cy.intercept功能略有不同,但上述代码可以实现类似的效果。

英文:

In Cypress, cy.intercept can be used for this purpose:

cy.intercept('GET', 'https://hello.com/script.js').as('scriptLoaded');
cy.wait('@scriptLoaded', {timeout: 20000});

How do I do that in Playwright?

答案1

得分: 1

你似乎正在寻找等待请求和/或响应的内容:https://playwright.dev/docs/api/class-page#page-wait-for-request

英文:

Think what You are looking for are wait for request and/or response : https://playwright.dev/docs/api/class-page#page-wait-for-request

huangapple
  • 本文由 发表于 2023年7月20日 20:48:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定