简体   繁体   中英

In Tritium, what are the differences between CSS and XPath selectors?

What are the advantages / disadvantages of the two different selectors?

Should I use one over the other?

I think it's primarily a matter of user preference.

To select the first child of all <p> elements, you'd do:

  • $("//p/*[1]") in Xpath
  • $$("p > *:first-child") in CSS

I prefer using Xpath, but YMMV.

Note that, internally, all CSS selectors are converted to Xpath. For example, the selector $$("#one") will be converted into $(".//*[id='one']") .

Just a few notes:

  • indexing starts from 1 in XPath, so it's //p/*[1]
  • the CSS selectors in Tritium allow you to prefix a selector with > , as in $$("> p > :first-child") ; this will be converted into a scoped search (ie, ./p/*[1] )
  • because CSS selectors are (currently) dynamically converted into XPath, there's a slight performance hit compared to using straight XPath

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM