Search code examples
puppeteerapify

How to pass json object to $eval function


I am trying to pass the json to the function with no success here is what I tried so far

await aHandle.$eval('input[name="date"]', (el) => (el.value =input.date)(input) );

or like this

await aHandle.$eval('input[name="date"]', (el) => (el.value =input.date),input );

or like this

await aHandle.$eval('input[name="date"]', (el) => (el.value =input.date) ).(input);

please advise the correct way


Solution

  • This works

    const Apify = require('apify');
    
    Apify.main(async () => {
        const q = await Apify.openRequestQueue();
        await q.addRequest({ url: 'http://example.com' })
    
        const c = new Apify.PuppeteerCrawler({
            requestQueue: q,
            handlePageFunction: async ({ page }) => {
                const inputServerSide = {
                    date: '2019-09-26'
                };
                const aHandle = await page.$('div');
                await aHandle.$eval('h1', (el, inputClientSide) => {
                    el.name = inputClientSide.date;
                }, inputServerSide );
                const name = await page.$eval('h1', (el) => el.name);
                console.log('Name:', name);
            }
        })
    
        await c.run();
    });