Reputation: 33
When to use Xpath , CSS and DOM locator in locating the element in selenium? I want to know the different scenarios to specific use of different locators. Why can not be Xpath will be helpful in all conditions to locate the elements?
Upvotes: 1
Views: 5165
Reputation: 3451
Selenium provided different locator strategies to make the code readable and performant in various situations.
1) You would use id=someId
if your element has id
. Using this syntax makes code more readable and simpler.
2) DOM locators are not used commonly mainly due to their clumsy/lengthy syntax.
3) CSS locators are good when the query is relatively simpler. They have the major advantage of being supported natively by most of the mordern browsers. Even though they have their own limitations like locating objects by index, unable to traverse up the hierarchy etc. which makes their usage difficult for complex queries.
4) XPath syntax is extremely powerful and you can formulate complex queries without much effort. Some browsers like IE don't support XPath natively hence evaluating XPaths expression using external XPath library makes it very slow.
Each of the locators have their own advantages and limitations. Now its up to you to decide which locator or combination of locator strategies would make your life simpler.
Hope this helps
Upvotes: 1