Home > Enterprise >  Playwright - get all child divs of div by div ids (TypeScript)
Playwright - get all child divs of div by div ids (TypeScript)

Time:03-09

I am reading through the PlayWright docs and haven't seen an example like the following.

If I have a div with 3 child divs each having an img in them, how can I get a list of the image refs?

<div data-testid="a">
  <div data-testid="1"><img src="http://test.com/one"></div>
  <div data-testid="2"><img src="http://test.com/two"></div>
  <div data-testid="3"><img src="http://test.com/three"></div>
</div>

I'd like to get a list of:

  • "http://test.com/one"
  • "http://test.com/two"
  • "http://test.com/three"

Based on a using a couple page.locator calls or whatever. All the examples I have seen in the docs tend to just use click() and fill().

Just looking for the general idea... pseudocode is perfect.

CodePudding user response:

I'd would solve it with an evaluate:

const div = page.locator('data-testid=a');
const urls = await div.evaluate(div => 
  Array.from(div.querySelectorAll('img')).map(img => img.getAttribute('src')));
  • Related