英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论