1

I am converting a old Selenium/C# test suite to Playwright/C# and it is going very well apart from one little issue, which is....

In the Selenium suite this code correctly returns the value from a text box

var tValue = Driver.FindElement(locator).GetDomProperty("value") ?? "";

However this code in the new Playwright suite when running the same test returns nothing

var tValue = await locator.GetAttributeAsync("value") ?? "";

This works fine for these attributes, only the attribute value is failing although it works in Selenium version.

tValue = await locator.GetAttributeAsync("type") ?? "";
tValue = await locator.GetAttributeAsync("class") ?? "";
tValue = await locator.GetAttributeAsync("id") ?? "";

Any ideas please

2
  • 2
    In Playwright, GetAttributeAsync("value") fetches the initial HTML attribute, not the current input value. For dynamic values (like text typed by users), use: csharp var tValue = await locator.InputValueAsync(); This mirrors the behavior of GetDomProperty("value") in Selenium.
    – Adel Alaa
    Commented 2 days ago
  • @AdelAlaa If you have an answer, put it in an answer not in comments.
    – JeffC
    Commented 2 days ago

2 Answers 2

2

You would be looking for inputValueAsync

Returns the value for the matching <input> or <textarea> or <select> element.

String value = await page.GetByRole(AriaRole.Textbox).InputValueAsync();

Generally speaking an element's attribute is not the same as it's property, and it's the value property that matters most when testing (if you've just caused an action to change it, like .fill()).

The attribute is useful to set the initial property value, in case that is what you need to test.

Notice the Selenium method is GetDomProperty() not GetDomAttribute().

See also the note about directly asserting the value, in case that if that is your goal:

Asserting value
If you need to assert input value, prefer Expect(Locator).ToHaveValueAsync() to avoid flakiness. See assertions guide for more details.

One more thing, you need to bracket the await expression when applying the Nullish coalescing operator (??) inline.

String value = (await page.GetByRole(AriaRole.Textbox).InputValueAsync()) ?? ''
0

Thanks for the hints and tips all, these led me to the answer which is .....

var tValue = locator.InputValueAsync().Result;

Sorted, many thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.