0

I'm trying to get data from a webpage using Playwright in TypeScript. I have a section of code where I want to extract information from multiple elements matching a certain locator.

However, I'm unsure whether to use Playwright's locator().all() method or evaluateAll() with querySelector. Both "just work", but I heard someone say don't use querySelector use locators.

Just to confirm that I'm using Playwright library, not @playwright/test.

Here's the code that uses a locator:

const ids = []; for (const id of await page.locator("div.ids").all()) { const id = await id.locator("span.id").first().textContent(); // Idk if this look ok or not but this's the only way I can come up with // because some time span.a will not exist and cause timeout const a = (await id.locator("span.a").count()) > 0 ? await id.locator("span.a").first().textContent() : null; idms.push({id, a}); } 

Here's the code that also use locator first but then use evaluateAll() and querySelector()

const ids = await page.locator("div.ids").evaluateAll((ids) => ids.map((id) => { const id = id.querySelector("span.id")?.textContent; const a = id.querySelector("span.a")?.textContent ?? null; return {id, a}; }) ); 
4
  • The code inside evaluteAll runs in the browser, so it's ok to use querySelector there. Commented Jan 5, 2024 at 12:17
  • @hardkoded Ah, thanks and it looks more readable and cleaner to me than the one above. Commented Jan 5, 2024 at 14:39
  • Please share a minimal reproducible example, preferably with the site and the expected output. If the page requires waiting, there's good reason to move this to a locator. There may be a better way to get that data than you assume here, but it's pretty much impossible to guess what that might be without seeing the site, since every one is unique. Thanks. Commented Jan 5, 2024 at 20:10
  • @ggorlen Sorry about that, the page just returned an HTML so I think it would be fine for now. If I come across a SPA that requires waiting I think that would be the reason to use locator Locator. But since the page right now is just an HTML, there doesn't seem to be any difference at the moment. Thanks! Commented Jan 6, 2024 at 5:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.