What is the fastest and slowest ways of finding elements using Selenium Webdriver?

佐手、 提交于 2020-02-29 04:51:20

问题


I've read plenty of forums and blogs but somehow I ain't convinced on this yet. Recently a friend of mine was asked this question in an interview.


回答1:


  1. ID: id will obviously be the most preferred attribute to select an element (unless it is generated). It is also the fastest way to locate an element since the id is unique.

  2. Name / tagName: This locator works well for finding multiple elements with the same name.

  3. Linktext / PartialLinkText - Locator is good for navigation activites.

  4. CSS vs Xpath -

You may go through this question for the CSS vs XPath debate. As per the answers, CSS is more readable, faster (especially in IE lower versions), and will work in all browsers.

A big plus with Xpath is that it supports 'contains' whereas it is deprecated in CSS (atleast in Selenium). But, 'contains' works like a charm with the Jsoup library and I preferred using Jsoup(CSS selector) over Xsoup(Xpath selector) for selecting elements in the document.

As far as Selenium is concerned, many prefer to use XPath (atleast this is what I've observed), but it has to be well formed so that it doesn't change a lot when web page changes.

Another good article would be this - http://elementalselenium.com/tips/32-xpath-vs-css



来源:https://stackoverflow.com/questions/29406279/what-is-the-fastest-and-slowest-ways-of-finding-elements-using-selenium-webdrive

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