2

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?

3

When IDs are dynamic (frequently database IDs) you can use the following strategies (listed in preferred order) to locate the element:

  • Add data attributes to existing elements in the source, e.g. data-product='first'
  • Add additional elements and/or attributes (e.g. name=) as fixed identifiers to the source
  • Use text content to identify the element
  • Use the full path, e.g. //body/table/tr/div/... etc. using :first or :last as needed
  • Use a relative path based on a nearby element that does have a fixed ID

All 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.

1

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:-

  • Using single attribute (//tagname[@attribute-name = 'value']).
  • Using multiple attribute (//tagname[@attribute-name1 = 'value1'][@attribute-name2='value2']).
  • Using contains method (//tagname[contains(@attribute-name, 'value')]).
  • Using starts-with method (//tagname[starts-with(@attribute-name, 'value')]).
  • Using following node (xpath/following::again-ur-regular-path). **example =>** //a[@href='xyz']/following::a[1]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.