I have to automate a system, which changes its ID's from time to time, and the XPath is not static.
What is the best way to automate this kind of system?
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It only takes a minute to sign up.
Sign up to join this communityWhen IDs are dynamic (frequently database IDs) you can use the following strategies (listed in preferred order) to locate the element:
data-product='first'
name=
) as fixed identifiers to the source//body/table/tr/div/
... etc. using :first
or :last
as neededAll of the above have advantages and disadvantages. It's up to you and your individual situation as to which will be most appropriate. Some will work in some situations other will not. You may need to mix and match several of the approaches for a given page to select different elements.
Whenever Id's are dynamic then CSS selector is the best way to locate an element, but if you want to use Xpath then you can use Dynamic Xpath methods like:-
(//tagname[@attribute-name = 'value'])
. (//tagname[@attribute-name1 = 'value1'][@attribute-name2='value2']).
(//tagname[contains(@attribute-name, 'value')]).
(//tagname[starts-with(@attribute-name, 'value')]).
(xpath/following::again-ur-regular-path). **example =>** //a[@href='xyz']/following::a[1]