0

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)


})()

1 Answer 1

1

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.