1

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for response Vaman. But can you please tell me when IE does not support XPath and rest of the locator are not helping to get that element,how can i proceed the test in IE. what would be solution then.
As far as selnium 1.0 and IE is concerned combination of CSS selectors and DOM apis are best option in my opinion. You can use external XPath library to work with XPath expressions but it will be poor in performance. Hope this helps. Just for FYI it is a good practice on stackoverflow to vote up and accept the answer (little tick mark to the left) if you are satisfied with the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.