Skip to content

CSS Selectors vs XPath

CSS selectors

Great for:

  • BeautifulSoup (selectselect, select_oneselect_one)
  • Playwright
  • browser devtools

Examples:

  • .price.price
  • div.card adiv.card a
  • #main h2.title#main h2.title

XPath

Great for:

  • lxml
  • Selenium (via XPath)
  • complex DOM relationships

Examples:

  • //div[@class='card']//a//div[@class='card']//a
  • //table//tr[1]/td[2]//table//tr[1]/td[2]

Quick comparison

  • CSS is usually simpler
  • XPath is more powerful for โ€œgo to parent/siblingโ€ cases

If this helped you, consider buying me a coffee โ˜•

Buy me a coffee

Was this page helpful?

Let us know how we did