> For the complete documentation index, see [llms.txt](https://qatesting.gitbook.io/qa/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://qatesting.gitbook.io/qa/automation-testing/flow/selenium/selenium-webdriver/webdriver-commands/webelement/locators/quick-reference-for-xpath-+-css.md).

# Quick Reference for XPath + CSS

## What are the different approaches to using XPath and CSS Selector?

<table data-full-width="true"><thead><tr><th>Description</th><th>XPath</th><th>CSS Selector</th></tr></thead><tbody><tr><td>Whole WebPage</td><td>/html</td><td>html</td></tr><tr><td>Whole WebPage body</td><td>/html/body</td><td>body</td></tr><tr><td>Image element</td><td>//img</td><td>img</td></tr><tr><td>Link</td><td>//a[@href = 'url']</td><td>a[href = 'url']</td></tr><tr><td>Direct Child</td><td>//div/a</td><td>div > a</td></tr><tr><td>Id</td><td>//tagName[@id=’idValue’]</td><td>tagName#idValue</td></tr><tr><td>Class</td><td>//tagName[@class=’classValue’]</td><td>tagName.Value of Class attribute</td></tr><tr><td>Attribute</td><td>//tagname[@attribute-name=’value1′]</td><td>tagName[attribute=Value of attribute]</td></tr><tr><td>Multiple Attributes</td><td>//input[@type='submit' and @name='btnLogin']</td><td>tagname[attribute1='value1'][attribute2='value2']</td></tr><tr><td>Contains</td><td>//*[contains(@type,'sub')]</td><td>tagname[attribute*=substring]</td></tr><tr><td>Starts with</td><td>//tagname[starts-with(@attribute, ‘Start value’)]</td><td>tagname[attribute^=prefix of the String]</td></tr><tr><td>Ends with</td><td>//tagname[ends-with(@attribute, ‘End value’)]</td><td>tagname[attribute$=suffix of the String]</td></tr><tr><td>Matches</td><td>//*[matches(@type,'value')]</td><td>N/A</td></tr><tr><td>First Child</td><td>//ul[@id=’list’]/li[1]</td><td>ul#list li:first-child</td></tr><tr><td>Last Child</td><td>//ul[@id=’list’]/li[last()]</td><td>ul#list li:last-child</td></tr><tr><td>nth Child</td><td>//ul[@id=’list’]/li[3]</td><td>ul#list li:nth-child(3)</td></tr><tr><td>Text Value</td><td>//td[text()=‘textname']</td><td>N/A</td></tr><tr><td>Element preceding some sibling</td><td>E2/preceding-sibling::E1</td><td>N/A</td></tr><tr><td>Sibling element immediately preceding</td><td>E/preceding-sibling::*[1]</td><td>N/A</td></tr><tr><td>User interface element that is disabled</td><td>E[@disabled]</td><td>E:disabled</td></tr><tr><td>Checkbox (or radio button) that is checked</td><td>//*[@checked]</td><td>*:checked</td></tr><tr><td>Text Value</td><td>//td[text()=‘textname']</td><td>N/A</td></tr></tbody></table>

## What are the differences between XPath and CSS locators?

### Traversal:

❎ XPath allows **bidirectional** traversal.

```java
//div[@id='id']//preceding::input
//div[@id='id']//following::input
```

➡️ CSS only moves forward.

```java
 div#id ~ input
```

👉 So XPath has more powerful traversal.

### Performance:

✅ CSS selectors are **faster** since browsers optimize CSS.

### Readability:

✅ CSS locators are more concise and readable.

```html
input[type='text']
```

```html
//input[@type='text']
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://qatesting.gitbook.io/qa/automation-testing/flow/selenium/selenium-webdriver/webdriver-commands/webelement/locators/quick-reference-for-xpath-+-css.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
