Selenium | Locate elements via custom attribute
briley last edited by
I'm new to test automation with Selenium (Python) and just wondering if it makes sense to locate elements on webpages via a custom attribute?
In some places the IDs of the elements are generated and have a new ID the next time the page is opened.
Names and classes can sometimes change and are not unique.
The developer offered me to add an additional attribute to the elements that I can use for the test automation.
``` Link Text ```
I found the idea quite charming because it is completely detached from developer purposes. This attribute is used explicitly for locating purposes in test automation.
Unfortunately, I have currently found only the way to address custom attributes via XPath. And that should be much slower than identifying elements by ID or name.
Can you please share your experiences with me?
Is there any library in Python that I can use to find such a custom attribute more efficiently?
Have you found a better way to locate elements?
When using selectors i try to assume nothing.
By.cssSelector("form button")is a bad selector because the frontend might change the button to an
If possible, i try to mark all my test intends with specific classes. Like
By.cssSelector(".e2e-contact-form .e2e-submit"). This only assumes that there are elements with these classes, independent of location or element type. Another advantage is, that when the frontend devs changes the design, your selector can stay the same. Frontend should use their own classes to change the design. Additionally, when frontend changes code surrounding your "e2e" classes, they are aware that this may impact e2e testing and can notify you in advance.
But i guess a custom attribute would work as well... though it bloats your css selectors to