How to Fetch CSS Selector & XPath of Any Field

Learn how to easily find and copy CSS selectors and XPath expressions for any element on a webpage using browser DevTools.

How to fetch CSS selector and XPath demonstration

What are CSS Selectors and XPath

CSS selectors and XPath are two different ways to identify and locate elements on a webpage. They are essential tools for web automation, testing, and scraping.

CSS Selectors

CSS selectors are patterns used to select HTML elements. They are the same selectors used in CSS styling, making them familiar to web developers. Examples include:

  • #myId - Selects element with id="myId"
  • .myClass - Selects elements with class="myClass"
  • button - Selects all button elements
  • input[type="text"] - Selects text input fields

XPath

XPath (XML Path Language) is a query language for selecting nodes from an XML or HTML document. It provides more powerful and flexible element selection capabilities. Examples include:

  • //button - Selects all button elements
  • //input[@type='text'] - Selects text inputs by attribute
  • //div[@class='container'] - Selects divs with specific class
  • //button[contains(text(), 'Submit')] - Selects button containing "Submit" text

How to Open DevTools

Developer Tools (DevTools) are built into all modern browsers and provide powerful debugging and inspection capabilities. Here's how to open them:

Keyboard Shortcuts

  • Windows/Linux: Press F12 or Ctrl + Shift + I
  • Mac: Press Cmd + Option + I or Cmd + Option + C

Alternative Methods

  • Right-click on any element and select "Inspect" or "Inspect Element"
  • Go to browser menu → More Tools → Developer Tools
  • In Chrome/Edge: Menu (⋮) → More Tools → Developer Tools
  • In Firefox: Menu (☰) → More Tools → Web Developer Tools

How to Select an Element

Once DevTools is open, you can select any element on the page in several ways:

Method 1: Element Selector Tool

  1. Click the Element Selector icon (cursor with box) in the top-left of DevTools
  2. Hover over elements on the page - they will be highlighted
  3. Click on the element you want to inspect
  4. The element will be selected in the Elements/Inspector panel

Method 2: Right-Click Inspect

  1. Right-click directly on the element you want to inspect
  2. Select "Inspect" or "Inspect Element" from the context menu
  3. The element will be highlighted in the Elements panel

Method 3: Search in Elements Panel

  1. In the Elements/Inspector panel, press Ctrl + F (or Cmd + F on Mac)
  2. Type text, class name, or ID to search for elements
  3. Navigate through matches to find your element

How to Copy CSS Selector

After selecting an element in DevTools, follow these steps to copy its CSS selector:

Chrome/Edge/Brave

  1. Right-click on the selected element in the Elements panel
  2. Hover over "Copy" in the context menu
  3. Select "Copy selector"
  4. The CSS selector is now in your clipboard

Firefox

  1. Right-click on the selected element in the Inspector panel
  2. Select "Copy""CSS Selector"
  3. The CSS selector is now in your clipboard

Safari

  1. Right-click on the selected element in the Elements panel
  2. Select "Copy""Copy Selector Path"
  3. The CSS selector is now in your clipboard

How to Copy XPath

Copying XPath follows a similar process to CSS selectors:

Chrome/Edge/Brave

  1. Right-click on the selected element in the Elements panel
  2. Hover over "Copy" in the context menu
  3. Select "Copy XPath" or "Copy full XPath"
  4. The XPath expression is now in your clipboard

Firefox

  1. Right-click on the selected element in the Inspector panel
  2. Select "Copy""XPath"
  3. The XPath expression is now in your clipboard

Safari

  1. Right-click on the selected element in the Elements panel
  2. Select "Copy""Copy XPath"
  3. The XPath expression is now in your clipboard

Tips for Better Selectors

  • Prefer IDs: If an element has a unique ID, use it. IDs are the most reliable and fastest selectors (e.g., #submit-button).
  • Avoid overly specific paths: Selectors like div > div > div > button are fragile. Use classes or data attributes instead.
  • Use data attributes: If you control the HTML, add data-testid or data-cy attributes for stable selectors.
  • Combine selectors: Use multiple attributes for more specific targeting (e.g., button[type="submit"][class="primary"]).
  • Test your selectors: Use the browser console to test selectors with document.querySelector() (CSS) or $x() (XPath in Firefox).
  • Consider XPath for complex queries: XPath is better for selecting elements based on text content or complex relationships.
  • Avoid auto-generated selectors: Browser-generated selectors are often too specific. Simplify them when possible.
  • Use relative XPath: Prefer //button over /html/body/div/button for more maintainable code.

Cheat Sheet

Selector TypeCSS SelectorXPathDescription
ID#myId//*[@id='myId']Select element by ID
Class.myClass//*[@class='myClass']Select element by class
Tagbutton//buttonSelect all elements of type
Attributeinput[type='text']//input[@type='text']Select by attribute value
Descendantdiv button//div//buttonSelect descendant elements
Direct Childdiv > button//div/buttonSelect direct child
Contains Textbutton:contains('Submit')//button[contains(text(), 'Submit')]Select by text content
Multiple Classes.class1.class2//*[contains(@class, 'class1') and contains(@class, 'class2')]Select element with multiple classes
Nth Childli:nth-child(2)//li[2]Select nth child element
Starts With[id^='prefix']//*[starts-with(@id, 'prefix')]Select by attribute prefix