Alexander Puchkov
Alexander Puchkov

Reputation: 5973

How to click a visible button when there are hidden buttons with the same text using Robot Framework Browser library?

Scenario: there are 4 buttons on the page with the same text (value). However, only one is visible, others are hidden.

A test like this:

Should display validations
  Click    "Save"

fails with the following error:

Error: locator.click: Error: strict mode violation: locator('text="Save"') resolved to 4 elements:

I can make the test work by updating a selector to include a specific parent element, e.g.:

Should display validations
  Click    id=modal--add >> "Save"

But I wonder if there's a way to target the only single visible button by text to keep the test more simple and generic.

Can't find any example in the Browser library documentation.

Note: this is a question about a new Browser library (Playwright), not Selenium.

Upvotes: 0

Views: 623

Answers (2)

jakub.bitman
jakub.bitman

Reputation: 5

Isn't it possible to use the not syntax? to find all buttons but the ones that are hidden? Of course only if those buttons have something like hidden='true'

Upvotes: 0

Todor Minakov
Todor Minakov

Reputation: 20077

In short, no; you can always turn off the failure with Set Strict Mode, and go for the first one.

But with text contains selector to aim at the "only visible" or n-th element? No.
That's what css and xpath are for.

Plus, if that was supported, or the element is the 1st matched one right now, think how stable is this strategy in the long run; the page may get a new functionality and the button you target to change its index. And now you'll have a case's functional failure due to unstable locator; while the latter should be as punctual as possible, and semi-constant.
Your 2nd/compound one looks good in this regard.

Upvotes: 0

Related Questions