您如何使用剧作家在浏览器上下文中导入本地模块?
这是我要做的事情的一个示例:
../ src/foo/bar.ts
是本地打字稿文件,
该文件中的代码旨在在浏览器上下文中运行(IT访问它 websockets
),因此我需要在 page.evaluate
page.evaluate( async () => {
const { namedExportExample } = await import('../src/Foo/Bar;)
// ...
});
我获取 page.evaluate:typeError:无法解决模块指定器
和i''毫不奇怪,我不确定浏览器如何评估该导入,但是我想知道是否有办法执行此操作?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于 @Max-Schmitt解释的原因,您无法导入模块。但是剧作家的“上下文”提供了一种在浏览器实例中注入新功能的方法。
示例
You can't import modules for reasons explained by @max-schmitt. But Playwright's "Context" provides a way to inject new functions in the browser instance.
https://playwright.dev/docs/api/class-browsercontext#browser-context-expose-function
Example
在您的情况下,您尝试将文件导入页面内,这对该文件的何处不了解。您需要将功能串起并将其传递给页面以进行评估。
有关更多信息,请参见此处: https://playwright.dev/docs/evaluating
In your scenario you try to import your file inside the page, which does not know anything about where to import that file. You need to stringify your function and pass it over the page to evaluate it.
See here for more information: https://playwright.dev/docs/evaluating