Home > Web Front-end > JS Tutorial > body text

Why Does `page.evaluate` Return Empty Objects When Using `querySelectorAll`?

Patricia Arquette
Release: 2024-11-13 13:50:02
Original
203 people have browsed it

Why Does `page.evaluate` Return Empty Objects When Using `querySelectorAll`?

Unexpected Empty Object Array Returned by page.evaluate querySelectorAll

When utilizing Puppeteer's page.evaluate function with querySelectorAll, users may encounter an issue where the returned array contains empty objects.

Cause:

The values returned from the page.evaluate function must be JSON serializable. By default, HTML elements are not JSON serializable without modifications.

Solution:

To resolve this issue, the extracted data from the HTML elements should be modified to a JSON serializable format. For instance, if the desired data is the href values of the elements, the following code snippet can be used:

await this.page.evaluate((sel) => {
    let elements = Array.from(document.querySelectorAll(sel));
    let links = elements.map(element => {
        return element.href;
    });
    return links;
}, sel);
Copy after login

This code extracts the href values from the elements and returns them as an array of strings, which is JSON serializable. By modifying the returned values, the issue of empty objects can be avoided.

The above is the detailed content of Why Does `page.evaluate` Return Empty Objects When Using `querySelectorAll`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template