Focus element with "data-xxx" method

Hello,

On a page, we have many attributes (like “id”, “name” etc).
But there are some page where this attribute are not present.
I resolved the issue by including on each HTML element a custom locator beginning by “data-”.
So I would like to use custom locator (ex : “data-subscription-email”) on my RF test but an arror message is appeared saying “locator data-subscription-email not found”.

Have you already seen this issue ?
Do you have any solution ?

I have read that I must define a custom locator strategy but without an example, I don’t understand how to do it.

Thanks a lot !

Hi John,

SeleniumLibrary I assume? - Please always state which library you are using, it make it much easier for people trying to help you.

If you don’t have recognised attributes then use one of the other locator methods. xpath will pretty much always work so it’s pretty useful did you try an xpath like //*[@data-subscription-email]

Hope that helps,

Dave.

Hi Dave,

Thanks for your answer.
You’re alright : I’m talking about SeleniumLibrary.

By using custom locators, I thought that I could focus an attribute as usually.
But I see that I must use “custom locator strategy” and I don’t know how to put it on my RF test.

Furthermore, in my head, using xpath is not a stable solution because if the path is modified, the test will be KO. Am I wrong ?

In my context, here is my test case :

Subscription form

Input Text    id=edit-mail    ${var}
Set Browser Implicit Wait    5
Input Password    **data-inscription-mdp**    ${AUTH}[password] 
Input Password    **data-inscription-confirmation-mdp**    ${AUTH}[password_confirm] 

Thanks !

Hi John,

This is a common perception, but it’s only true when using poorly constructed xpaths, it’s a bit like saying bicycles are unstable because they only have 2 wheels it’s a lazy excuse.

Lets take the example of the title of this thread Focus element with "data-xxx" method If we just take the default xpaths provided by the dev tools we get:
(copy full xpath) /html/body/section/div/div[3]/div[2]/div[3]/div/div/h1/a and
(copy xpath) //*[@id="topic-title"]/div/div/h1/a

The first one is pretty mu guaranteed to fail if anything on the page changes.

The second one is better and less prone to issues, but likewise we could construct a much simpler xpath of:
//h1/a
which is likely to continue working regardless of how the developers restructure the page, unless they start using links inside heading 1 tags all over the place, in which case I would then change the xpath to:
//*[@id="topic-title"]//h1/a

which is a simplified version of the second one provided by the browser but less prone to
breaking when the page structure changes.

Take your time to learn how to construct your own xpaths and as it will enable you to construct more robust test cases, Start with this page (XPath Syntax)

For your specific test case these simple xpaths may be all you need:

Input Password    //@data-inscription-mdp    ${AUTH}[password] 
Input Password    //@data-inscription-confirmation-mdp    ${AUTH}[password_confirm]

It’s useful to know with SeleniumLibrary if your locator string starts with // then it automatically switches to xpath but you can always write it like this too if you prefer:

Input Password    xpath://@data-inscription-mdp    ${AUTH}[password] 
Input Password    xpath://@data-inscription-confirmation-mdp    ${AUTH}[password_confirm]

Hope this helps,

Dave.

Hi Dave,

Thank you for this instructive reply.
I’m happy to see that using xpath is not prohibited but I just need to learn how to use it.

For my specific test case, I have written this :

Input Password    //*[@data-inscription-mdp="data-inscription-mdp"]    ${AUTH}[password]
Input Password    //*[@data-inscription-confirmation-mdp="data-inscription-confirmation-mdp"]    ${AUTH}[password_confirm]

and it works ! Comparing to your propositions, I have to fill the value of “data-confirmation-mdp” attribute.
Do you confirm that it will works even if the path is going to be changed ?

Thanks again for your time because I try to adopt RF into my organisation and I asking a lot of questions (in poor english, I confess) and it’s with answers like yours that the learning is better :slight_smile:

Hi John,

Your xpaths should be fine as long as the developer doesn’t do one of these things to those fields:

  • change the data-inscription* attributes to something else
  • remove the data-inscription* attributes from these fields
  • add extra fields that also have these same attributes

if one of these things happen you will need to adjust your xpath, but if you were using the one of the other locator methods and one of these things happened you would probably have to change the locator too. also if one of these things happened it’s quite possible its a bug, so instead of changing your robot file you would raise a defect and say your test was successful in detecting an unplanned change.

I wondered if English was not your main language, it’s ok many people who use RF are have English as a second, third, fourth, etc language and I have found the community is quite friendly and forgiving if you English is not perfect. it’s a nice change from other forums where the language police are regularly out in force.

Good luck with your adopting RF into your organisation, I believe it is the easiest test automation tool (I have used quite a lot of test automation tools) so I hope it’s successful for you. And feel free to ask questions here in the forums thats what forums are for, and remember “the only stupid question, is the one you never asked” (it’s an English joke) :rofl:

Dave.