Get an attribute or input value
Read an attribute, a form field value, or an element's text into a PHP variable.
Sometimes you need a value out of the page as data: an attribute, the current contents of a field, or an element's text. Each has its own reader on the locator. Pick the one that matches what you want.
Read an attribute
getAttribute() returns the value of an HTML attribute, or null when the attribute is absent.
$href = $page->getByRole('link', ['name' => 'Invoice'])->getAttribute('href');
if (null !== $href) {
echo $href.PHP_EOL;
}
Check for null. A missing attribute is not the same as an empty one.
Read the value of a form field
inputValue() returns the current value of an input, textarea, or select. This is the live value, including anything the user or a script typed, which the value attribute does not reflect.
$email = $page->getByLabel('Email')->inputValue();
Use inputValue() for form fields. Use getAttribute('value') only when you specifically want the original HTML attribute, not the current state.
Read an element's text
Two readers return text, and they differ:
textContent()returns the raw text of the node, including hidden parts, ornullwhen the node has none.innerText()returns the rendered text the browser shows, with hidden nodes dropped and whitespace normalized.
$raw = $page->getByRole('heading')->textContent();
$visible = $page->getByRole('heading')->innerText();
Reach for innerText() when you want what the user sees, textContent() when you want the underlying text regardless of styling.
These read once
None of these retry. They sample the page when you call them. If the value arrives after an action, assert on the element first so it has settled, then read:
use function Playwright\Testing\expect;
expect($page->getByLabel('Email'))->toHaveValue('ada@example.com');
$email = $page->getByLabel('Email')->inputValue();
Expected result
The variable contains the current page value at the time you read it. If the value is produced asynchronously, assert the expected state first, then read it for downstream PHP logic.
Go next
- Extract text from matching elements: read text across many matches
- Wait for text to appear: assert on text instead of reading it
- Forms and controls: filling and reading form fields