CSS and XPATH selector strategy

Introduction to inspect and evaluate

Google Chrome provides a built-in debugging tool called “Chrome DevTools” out of the box, which includes a handy feature that can evaluate or validate XPath/CSS selectors without any third party extensions.

This can be done by two ways:

Inspecting CSS Selectors and XPath Using Google Chrome DevTools

Step-by-Step Guide with Examples

Open Google Chrome DevTools

Open Google Chrome and navigate to the webpage you want to inspect. Right-click on the webpage and select “Inspect” from the context menu. Alternatively, you can press Ctrl+Shift+I (Windows/Linux) or Cmd+Option+I (Mac) to open the DevTools. The DevTools will open with the Elements panel active, displaying the HTML structure of the page.

Finding Elements Using CSS Selectors

Press Ctrl+F (Windows/Linux) or Cmd+F (Mac) to open the search box in the Elements panel.

Enter a CSS selector in the search box to find matching elements.

Example 1: Select an Element by Class Name

To find an element with the class nav-item, type .nav-item.

.nav-item

class selector

Example 2: Select an Element by ID

To find an element with the ID submit-button, type #submit-button.


#submit-button

id selector

Example 3: Select an Element by Attribute

To find an input element with the type text, type input[type=“text”].


input[type="text"]

css selector

Search for area label in textarea

css selector

textarea[aria-label$="Search"]
textarea[aria-label$="Search"][class="gLFyf"]

Finding Elements Using XPath

With the search box still open, enter an XPath expression to find matching elements.

Example 1: Select an Element by Tag Name

To find all button elements, type //button.

//button

Example 2: XPATH:Select an Element by Attribute

To find an input element with the type text, type //input[@type=“text”].

//input[@type="text"]

(xpath selector

Example 3: Select an Element by Text Content

To find a button element containing the text “Submit”, type //button[contains(text(), ‘Submit’)].

//button[contains(text(), 'Submit')]

Lets take another example with Gmail text

(xpath selector

//*[text()[contains(.,Gmail)]]

another more difficult example

(xpath selector dynamics

In this MS Dynamics example I looking for a unique item in the Employee master.

This example is an xpath query

 //div[contains(@controlname,'First')]/descendant::input

Here’s a step-by-step explanation:

So //div[contains(@controlname,‘First’)] selects all

elements whose controlname attribute contains the text ‘First’.

What about /descendant::input:

It is like sql you have to get used to it and it takes time but once you get accustomed it will be an addon compared to the various recording tools. Especially if you have to do advanced querying in dynamic “shadow” dom.

Important note

If there are matched elements, they will be highlighted in DOM. However, if there are matching strings inside DOM, they will be considered as valid results as well. For example, CSS selector header should match everything (inline CSS, scripts etc.) that contains the word header, instead of match only elements. For selenium/playwright and other tools with browser automation it is essential to understand that the result is always 1. If it has more then one click,you cannot recognize it.