Rayrun
← Back to Discord Forum

How to locate an input by its value

patrickberkeleyposted in #help-playwright
Open in Discord
patrickberkeley
patrickberkeley

Is it possible to locate an input by its value? I know the recommendation is to use the getBy… locators. But in this case there are multiple inputs on the screen with the same data-test selector and same label.

This thread is trying to answer question "Is it possible to locate an input by its value when multiple inputs have the same data-test selector and label?"

9 replies
garland2953

you could use xpath for that locator, but make sure your query is resilient to changes in the dom

Would assert XPath or some variant of a locator, they will only get to the item desired. You have to find the way to identify the right one. If one said i have 15 numbers 7 '1s, and 8 2's... Now i want a specific number by its' value which for the 15 numbers i only have two unique values of 1 or 2, with this limitation how do you get and match the right one.? If they are all the same, neither an XPath or a locator will help.

you might try using an unique css selector , e.g. await page.locator('input[value="something"]')

garland2953
@d3333333: it could work, you can use an nth match operator in the query, however that could make it flaky

it could work, you can use an nth match operator in the query, however that could make it flaky

@garland2953 How? How or why suggest "It could work", sorry test need to be 100% deterministic?

butchmayhew
butchmayhew

This was my solution - https://playwrightsolutions.com/how-do-i-check-the-value-inside-an-input-field-with-playwright/ This may not solve your entire issue but may give some ideas

garland2953
@d3333333: as in it will work, but if for whatever reason you have a list of 5 of the same labels and dom changes your nth match might not be matching to what you expect it to

as in it will work, but if for whatever reason you have a list of 5 of the same labels and dom changes your nth match might not be matching to what you expect it to

patrickberkeley
patrickberkeley

Thanks for all the suggestions. I asked this same question in playwright slack, and it was pointed out that if the inputs are not uniquely identifiable by anything other than their values, they 1) don't pass accessibility, 2) should be refactored to be unique, then just use of of the recommended getBy... locators.

@garland2953: I might have a different definition that "It will work", not really it is boolean it will either work 100% of the time or not. Not sure why you think having buggy tests that will never work is good.

I might have a different definition that "It will work", not really it is boolean it will either work 100% of the time or not. Not sure why you think having buggy tests that will never work is good.

Answer

Sure, you can locate an input by its value with @playwright/test, even when multiple inputs share the same data-test selector and label.

Here's how you can do it:

Using data-testid Attribute

Playwright supports shorthand for selecting elements using certain attributes, including data-testid. You can use the page.locator() method with the attribute selector to find the desired input element.

await page.locator('data-testid=username').fill('value');

This code fills an input element with the data-testid attribute set to "username" with a specified value.

Chaining Selectors

Playwright recommends chaining locators instead of relying solely on data-test selectors. Selectors can be combined with the >> token (selector1 >> selector2 >> selectors3). Each subsequent selector is queried relative to the previous one's result. This helps you locate specific elements even when they have similar attributes or labels.

await page.locator('css=article >> css=.bar > .baz >> css=input[value="example"]').click();

In this example, we first select an article element, then find a descendant element with class "bar" and child class "baz", and finally locate an input element with a specific value ("example").

By using these techniques, you can effectively locate an input by its value even in scenarios where multiple inputs have the same data-test selector and label.

Remember, these examples are based on the provided context. If you have additional details or requirements specific to your use case, please provide more information for further assistance.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.