0

Selenium WebDriver provides numerous ways to select an element as shown in googledocs (under "implements interfaces"):

  • by class name
  • by id
  • by xpath
  • etc.

Is there one that is more "resistant" (i.e. automated tests won't break) to changes in the application being developed and why?

2

As the other answer already said, the preferred order is this:

  1. ID Locator
  2. NAME Locator
  3. CSS Locator
  4. XPATH Locator

However, the reason why is simple: an ID is supposed to be unique, so once you have a certain element with a specific identifier, this is unlikely to change. Also, no matter where the element is moved to on a page, the ID stays the same.

However, an XPath is more brittle: for example, if elements switch places in a div or body container (due to new elements being added, ...), the xpath might break. Of course, you'll often have to rely on XPath or CssSelectors for dynamic or complex HTML.

Conclusion: try to get the developers to add as much ID and name elements as possible.

0

I have always been told this order when finding elements.

  1. By ID
  2. By CSS Selector
  3. By XPath

This has served me well with my automation in the past, be especially careful with XPath.

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.