mardi 31 mars 2020

Puppeteer not executing anonymous function in a page

For my tests, I would like to login to this page: https://www.ebay-kleinanzeigen.de/m-einloggen.html

When first requested, this page returns a page like the following:

<html><head><meta charset="utf-8">
  <script>
    function(){/* some logic*/}();
  </script>
</head><body></body></html>

This script has functions and an anonymous function that should be executed when the browser loads the page.

In a normal browser, this function fires a xhr request (where the server will set cookies) and then reloads the same page, that thanks to the cookies will contain the login form.

To see this in action, open a private tab in your favorite browser, open the dev tools, set the networking logs to persist and visit the page. The first network requests will look like this: dev tool

Using the following Puppeteer script, the browser doesn't execute the anonymous function and gets stuck waiting for the login form, that never appears:

import puppeteer from 'puppeteer';

const main = async () => {
    try {
        const browser = await puppeteer.launch({devtools: true});
        const page = await browser.newPage();
        await page.goto('https://www.ebay-kleinanzeigen.de');
        await page.waitForSelector('#login-form', { visible: true });
        await page.screenshot({path: 'login.png', fullPage: true})
        await browser.close();
    } catch (e) {
        console.log('error',e);
    }

}

main();

I can't use page.evaluate because the content of the function is dynamically created by the server.

Is there a way to let this anonymous function get executed at page load?

Aucun commentaire:

Enregistrer un commentaire