0

2 questions in 1:
1) In all the Selenium tutorials they divide the ways of finding elements in the way like:
1. By ID
2. By CLASS
3. By NAME
4. By TAGNAME
5. By CSS Selector 6. By Link
7. By XPath

I understand there are special functions in Find.By class in Selenium for each of these, but are all of them really different ways of specifying elements in the DOM or ID, Class and Name are just a special cases of TagNames?
2) And in the similar way:
Are Xpath and CSS are the only actual 2 different approaches to search for elements in the DOM while Name, Class, Tag, Text, LinkText are just parameters used to specify the elements?

4

Question 1

No, a tag name is what you find in brackets:

<tagName></tagName>

It is a shortcut for the CSS selector tagName

Name, ID and class are all attributes of an element, they are simply shorthand for specific CSS selectors you could use. However, with this I don't mean they are executed like this behind the scenes (see Question 2 below).

  • ID replaces the CSS selector #foo
  • Name replaces the CSS selector [name='foo']
  • Class replaces the CSS selector .foo

Question 2

Look at these source files: https://github.com/SeleniumHQ/selenium/tree/c10e8a955883f004452cdde18096d70738397788/javascript/atoms/locators

It will show you how each locator finds a match. For example, the LinkText locator first finds all links (by tag name a) and then filters on the text.

  • I would that performance is an important issue here. Everthing depends on how the implementation is, but the it is POSSIBLE to implement faster search for ID than others types of selectors - tagname would be faster than class was well. For a pure CSS/XPATH search, one can implement a pre-search analysis for transforming it to a more specific search, e.g. if one try to search for "#my-id", it can be transformed into ID."my-id". – João Farias Nov 10 '17 at 11:20
  • Locating by CSS is used by styles, so I would assume someone is spending lots of efforts to make it fast. XPATH, not so much. I (on advice of many experts on internet) try to avoid it as much as I can because it is brittle. So I assume there is less pressure to make it fast. But at least, OP has the order of preference right :-) – Peter M. Nov 10 '17 at 15:14

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.