Extract text from matching elements
Read every match of one locator into a PHP array, then iterate over it.
One locator can match many elements. When you need their text as data, not as an assertion, read all matches into a plain PHP array and loop over it.
Read every match at once
allTextContents() returns the textContent of each matched node as an array of strings. allInnerTexts() returns the rendered innerText instead, which respects visibility and collapses whitespace the way the browser shows it.
$names = $page->getByRole('listitem')->allTextContents();
foreach ($names as $name) {
echo trim($name).PHP_EOL;
}
The locator matches every list item. The array holds one entry per match, in document order.
Choose innerText when you want the visible text
allInnerTexts() gives you what the user sees: hidden nodes drop out and whitespace is normalized.
$labels = $page->getByRole('button')->allInnerTexts();
$enabled = array_filter($labels, static fn (string $label): bool => '' !== trim($label));
Use allTextContents() when you need the raw markup text, allInnerTexts() when you need the rendered text.
These are snapshots, not assertions
Both methods read the page once and return. They do not retry. If the list is still loading, you can capture too few rows.
use function Playwright\Testing\expect;
expect($page->getByRole('listitem'))->toHaveCount(5);
$rows = $page->getByRole('listitem')->allTextContents();
Assert the count first so the list has settled, then read. See assert a list count for why counting too early fails.
Expected result
The PHP array contains one string per matched element, in document order. Use it for reporting, export checks, or custom comparisons after the list has reached the expected state.
Go next
- Assert a list count: wait for the right number of matches before you read
- Get an attribute or input value: read a single element instead of many
- Core concepts: how one locator matches many elements