【问题标题】:Puppeteer - Remove function exposed by exposeFunction?Puppeteer - 删除由exposeFunction 公开的函数?
【发布时间】:2020-03-27 00:00:59
【问题描述】:

我正在以下列方式使用exposeFunction-command:

await this.page.exposeFunction('foo', function(){ return 'bar'; });

这按预期工作,并为我提供了 window.foo 功能。

如果我再次调用此代码,我会收到以下错误:

Error: Failed to add page binding with name foo: window['foo'] already exists!

在使用 page.goto() 导航时,此错误甚至仍然存在。

有没有办法解除对exposeFunction()暴露的函数的绑定?

【问题讨论】:

    标签: javascript node.js puppeteer webautomation


    【解决方案1】:

    您可以更改存储在_pageBindings 映射中的函数。这很 hacky,因为您会更改内部变量,但这是解决此问题的唯一方法。

    await this.page.exposeFunction('foo', function(){ return 'bar'; }); 
    this.page._pageBindings.set('foo', function(){ return 'baz'; });
    

    【讨论】:

    • 直接更改内部变量有什么缺点吗?它会影响性能吗?在这里对不起初学者。
    • @vexingCoder 风险在于这不是 API 的一部分。因此,木偶师可以根据需要重命名_pageBindings,从而破坏您的代码。
    • 哦,我明白了,非常感谢。因此,如果 puppeteer 改变了它,它就无法更新我的 puppeteer 版本。因为它会破坏我的代码谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多