【发布时间】:2019-05-03 00:44:56
【问题描述】:
我有一个 Angular 网站,我想使用 puppeteer 对其进行一些操作并截取屏幕截图。 我需要在页面加载后调用一个角度函数。
如果我在 chrome 中打开我的页面,我可以调用我的角度函数:
ng.probe($$('app-home')[0]).componentInstance.myFunction()
但是对于 puppeteer,如果我尝试这样做:
await page.evaluate("ng.probe($$('app-home')[0]).componentInstance.myFunction()")
我有一个错误提示 Error: Evaluation failed: ReferenceError: $$ is not defined
我试图在我的角度组件中声明一个 js 函数:
var s = document.createElement("script");
s.type = "text/javascript";
s.text = "function launchMyFunction() {" +
"ng.probe($$('app-home')[0]).componentInstance.myFunction()" +
"}"
this.elementRef.nativeElement.appendChild(s);
使用 Chrome 开发工具控制台,这可以正常工作。使用 puppeteer,我可以调用该函数,但我不断收到 Error: Evaluation failed: ReferenceError: $$ is not defined 消息。
如果我尝试更改我的功能,例如记录某些内容,它可以正常工作,问题似乎是 ng.probe。
有没有办法从 puppeteer 调用角度函数?
感谢您的宝贵时间
【问题讨论】:
标签: angular function puppeteer