简体   繁体   中英

How to call an on page function with playwright?

I'm using playwright to scrape some data from a page. I need to call a page function in order to change the browser's state to collect additional info.

What's the syntax for getting a function name from a data attribute and then calling that function on the page?

I keep getting the error: UnhandledPromiseRejectionWarning: page.evaluate: Evaluation failed: TypeError: cb is not a function

Here's what I have so far:

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)


})()

I think it comes down to either window[cb]() or eval(cb) since you're passing in a string with the function name.

Some reading on this topic:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM