Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to use map function in $$ #6447

Closed
ortoniKC opened this issue May 6, 2021 · 2 comments
Closed

[Question] How to use map function in $$ #6447

ortoniKC opened this issue May 6, 2021 · 2 comments

Comments

@ortoniKC
Copy link

ortoniKC commented May 6, 2021

Hi, I am trying to print a list of element text. It works when I for, but if I do the same in foreach, map, or filter it's not working.
Kindly let me know what am doing wrong here.

Sample code:

        await page.goto("https://letcode.in/elements");
        await page.fill("input[name='username']", "ortonikc");
        await page.press("input[name='username']", "Enter");
        await page.waitForSelector("app-gitrepos ol li", {
            timeout: 5000
        });
        const repos = await page.$$("app-gitrepos ol li");
        console.log(repos.length);
        // this works
        for await (const repo of repos) {
            console.log(await repo.innerText());
        }
        // This is not working
        repos.map(async (ele, i) => {
            const text = await ele.innerText();
            console.log(text);
        })
@ortoniKC ortoniKC changed the title [Question] How to use map functions in $$ [Question] How to use map function in $$ May 6, 2021
@aslushnikov
Copy link
Contributor

@ortoniKC when you pass an async function to the repos.map, the result would be a Promise. So you'll probably need to await all promises. For example, to get an array of inner texts:

const innerTexts = await Promise.all(repos.map(async (ele, i) => {
  return await ele.innerText();
}));

@ortoniKC
Copy link
Author

ortoniKC commented May 7, 2021

Thank you so much. I understood.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants