CSS Selector Syntax

The HTML Under Test

<div class="product-card" data-id="101">
  <h3 class="title">Nike Revolution 7</h3>
  <span class="price">₹1,299</span>
  <button id="add-to-cart" class="btn btn-primary" data-testid="add-101">Add to Cart</button>
</div>

Basic CSS Patterns

PatternSelectorMatches
id#add-to-cartthe button
class.product-cardthe card div
several classes.btn.btn-primaryelement with both classes
tag + classspan.pricethe price
descendant (space).product-card .titleh3 anywhere inside the card
direct child (>).product-card > buttonbutton that is a direct child
nth child.product-card:nth-child(2)second card in its parent
String name = driver.findElement(By.cssSelector(".product-card .title")).getText();
String price = driver.findElement(By.cssSelector("span.price")).getText();