I have read this Get Text topic about the same issue, but honestly, it didn’t help me. Previously I got an advice about xpaths and how to not make them too long. I tried that. That’s why I tried following:
Click css=span.omni-button-wrapper >> text=“Tallenna ja sulje” (Save and close in English)
Background:
There is just one that kind of button in the page
There are other similar buttons in every document I should test
I tried to solve problem modifying xpath shorter. I don’t have an experience about that, so this looks possibly horrifying for you. However, here it is:
Surprisingly, I got similar result with this. I’d like to ask two things:
First: how do I recognize the correct element in this kind of situations
The second: Is there any materiel what I could use to study. I mean, how I could create precise and correct selectors in these cases.
I am using Browser Guidebook, Playwright guides and so on, but with this experience of mine generic guidelines are little bit hard to bring in practice. Especially when real life is more complicated than examples.
You can use the SelectorsHub browser extension as a starting point for creating selectors. It does a decent job in generating better selectors than the default browser developer tools in general.
If doing it manually, you need to view the HTML markup and work your way up from the element you are trying to target. See what kind of parent/wrapper elements (sometimes referred as containers) are available. Sometimes those have an id or a class attribute that helps with restricting the selector to that particular section of the page.
SelectorsHub works in a similar way: It tries to find a selector that is specific-enough but omits all the unnecessary parts. Sometimes the end result is good as-is. Sometimes you can improve it manually.
When contains function is used with “.” it searches a string within an element, and all elements below it. So you can use it to locate any element above it.
I have asked about that possibility and hopefully this discussion leads to change. We are going to talk about these challenges with development teams soon.