Element locator use in selenium Xpath,CSS, DOM

蓝咒 提交于 2019-12-06 14:08:12

问题


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:


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



来源:https://stackoverflow.com/questions/6129355/element-locator-use-in-selenium-xpath-css-dom

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!