【问题标题】:How to call an on page function with playwright?如何用剧作家调用页面功能?
【发布时间】:2023-03-13 18:14:01
【问题描述】:

我正在使用剧作家从页面中抓取一些数据。我需要调用一个页面函数来更改浏览器的状态以收集更多信息。

从数据属性中获取函数名然后在页面上调用该函数的语法是什么?

我不断收到错误消息:UnhandledPromiseRejectionWarning: page.evaluate: Evaluation failed: TypeError: cb is not a function

这是我目前所拥有的:

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

(async()=>{

    this.browser = await chromium.launch({
            headless: true,
        });
        this.context = await this.browser.newContext();
        this.page = await this.context.newPage();
        this.page.goto('http://fakeExample.org')
    const callbackHandle = await this.page.$('[data-callback]');
    const cbName = await callbackHandle.evaluate(element=>element.getAttribute('data-callback')); //returns actual page function name 'myPageFunction'

    this.page.evaluate((cb) => {
        cb() //should call myPageFunction() on the page
    }, cbName)


})()

【问题讨论】:

    标签: javascript dom puppeteer playwright


    【解决方案1】:

    我认为它归结为window[cb]()eval(cb),因为您传递的是带有函数名称的字符串。

    关于这个主题的一些阅读:

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 2021-06-28
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 2022-08-03
      • 2021-04-20
      相关资源
      最近更新 更多