How can I use page inside of my page.evaluate?

Eric

Inside of this method that works with puppeteer, I want to use page but I can't quite work out how to get it inside the Promise.

async scrollList( page ) {
    let results = await page.evaluate( async () => {
        results = await new Promise( async resolve => {
            // next line fails :-(
            await page.mouse.move( 100, 100 );

            // do other stuff
            return resolve( results );
        });
    });

    console.log('Got results: ', results);
}

How can I do this? Right now I'm getting an error saying page is undefined.

Thomas Dondorf

Problem

You cannot access page as the two runtimes (Node.js and browser) are separated. The page object can only be accessed from within the Node.js runtime.

Solution

You have to expose a function that can access the page object to do that via page.exposeFunction:

await page.exposeFunction('moveMouse', async (x, y) => {
    // page is accessible here
    await page.mouse.move(x, y);
});

You can use that function inside page.evaluate by calling window.moveMouse(100, 100).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use imported function inside page.evaluate in Puppeteer with Jest?

How can I use is_page() inside a plugin?

How can I use a variable on my extended twig page (Symfony)?

Go How can I use JavaScript code on my html page

How can I use only one section for my home page

How can I use URL in my html page in django?

How can I evaluate a page retrieved by an url form a parent page in puppeteer`s evaluate function

How can I use AJAX to show in my HTML page the output of my PHP page?

Why can`t i use $scope.feedback.myChannel inside my controller if i can use it in my html page?

Can I use @media in my HTML page?

How can I get rid of keycloak's default login page and use my own login page

How to execute page.render inside page.evaluate?

How can i connect my page with Controller

How can I use lambdify to evaluate my function?

How can I make a button redirect my page to another page?

How can I check if page is my admin setting page?

How can I have refresh page link/button inside my JavaScript code?

How can I call a function defined inside a module of requirejs from my page?

How can I give background color to the text content inside a div in my html page.

Razor Pages: Page with partialview inside. How can i store my Data for the partialview?

How can I make the code inside my useEffect react hook re-run when page refreshes?

Nodejs/Puppeteer - How to use page.evaluate

How can I use parallel for in page load?

how can i use uipagecontrol for a page with actions?

How can i use ternary if in aspx page?

How can I use the value of variables defined inside a php class in another page

How can i limit my blogs per page on my website?

How can I set my slider at the bottom of my page?

How can I position my carousel at the bottom of my page?