--- id: selectors title: Selectors --- The [WebDriver Protocol](https://w3c.github.io/webdriver/) provides several selector strategies to query an element. WebdriverIO simplifies them to keep selecting elements simple. Please note that even though the command to query elements is called `$` and `$$`, they have nothing to do with jQuery or the [Sizzle Selector Engine](https://github.com/jquery/sizzle). While there are so many different selectors available, only a few of them provide a resilient way to find the right element. For example, given the following button: ```html ``` We __do__ and __do not__ recommend the following selectors: | Selector | Recommended | Notes | | -------- | ----------- | ----- | | `$('button')` | 🚨 Never | Worst - too generic, no context. | | `$('.btn.btn-large')` | 🚨 Never | Bad. Coupled to styling. Highly subject to change. | | `$('#main')` | ⚠️ Sparingly | Better. But still coupled to styling or JS event listeners. | | `$(() => document.queryElement('button'))` | ⚠️ Sparingly | Effective querying, complex to write. | | `$('button[name="submission"]')` | ⚠️ Sparingly | Coupled to the `name` attribute which has HTML semantics. | | `$('button[data-testid="submit"]')` | ✅ Good | Requires additional attribute, not connected to a11y. | | `$('aria/Submit')` or `$('button=Submit')` | ✅ Always | Best. Resembles how the user interacts with the page. It is recommended to use your frontend's translation files so your tests never fail when the translations are updated | ## CSS Query Selector If not indicated otherwise, WebdriverIO will query elements using the [CSS selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors) pattern, e.g.: ```js reference useHTTPS https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.js#L7-L8 ``` ## Link Text To get an anchor element with a specific text in it, query the text starting with an equals (`=`) sign. For example: ```html reference https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.html#L3 ``` You can query this element by calling: ```js reference useHTTPS https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.js#L16-L18 ``` ## Partial Link Text To find a anchor element whose visible text partially matches your search value, query it by using `*=` in front of the query string (e.g. `*=driver`). You can query the element from the example above by also calling: ```js reference useHTTPS https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.js#L24-L26 ``` __Note:__ You can't mix multiple selector strategies in one selector. Use multiple chained element queries to reach the same goal, e.g.: ```js const elem = await $('header h1*=Welcome') // doesn't work!!! // use instead const elem = await $('header').$('*=driver') ``` ## Element with certain text The same technique can be applied to elements as well. Additionally, it is also possible to do a case-insensitive matching using `.=` or `.*=` within the query. For example, here's a query for a level 1 heading with the text "Welcome to my Page": ```html reference https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.html#L2 ``` You can query this element by calling: ```js reference useHTTPS https://github.com/webdriverio/example-recipes/blob/13eddfac6f18a2a4812cc09ed7aa5e468f392060/selectors/example.js#L35C1-L38 ``` Or using query partial text: ```js reference useHTTPS https://github.com/webdriverio/example-recipes/blob/13eddfac6f18a2a4812cc09ed7aa5e468f392060/selectors/example.js#L44C9-L47 ``` The same works for `id` and `class` names: ```html reference https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.html#L4 ``` You can query this element by calling: ```js reference useHTTPS https://github.com/webdriverio/example-recipes/blob/13eddfac6f18a2a4812cc09ed7aa5e468f392060/selectors/example.js#L49-L67 ``` __Note:__ You can't mix multiple selector strategies in one selector. Use multiple chained element queries to reach the same goal, e.g.: ```js const elem = await $('header h1*=Welcome') // doesn't work!!! // use instead const elem = await $('header').$('h1*=Welcome') ``` ## Tag Name To query an element with a specific tag name, use `` or ``. ```html reference https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.html#L5 ``` You can query this element by calling: ```js reference useHTTPS https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.js#L61-L62 ``` ## Name Attribute For querying elements with a specific name attribute you can either use a normal CSS3 selector or the provided name strategy from the [JSONWireProtocol](https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol) by passing something like [name="some-name"] as selector parameter: ```html reference https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.html#L6 ``` ```js reference useHTTPS https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.js#L68-L69 ``` __Note:__ This selector strategy it deprecated and only works in old browser that are run by the JSONWireProtocol protocol or by using Appium. ## xPath It is also possible to query elements via a specific [xPath](https://developer.mozilla.org/en-US/docs/Web/XPath). An xPath selector has a format like `//body/div[6]/div[1]/span[1]`. ```html reference https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/xpath.html ``` You can query the second paragraph by calling: ```js reference useHTTPS https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.js#L75-L76 ``` You can use xPath to also traverse up and down the DOM tree: ```js reference useHTTPS https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.js#L78-L79 ``` ## Accessibility Name Selector Query elements by their accessible name. The accessible name is what is announced by a screen reader when that element receives focus. The value of the accessible name can be both visual content or hidden text alternatives. :::info You can read more about this selector in our [release blog post](/blog/2022/09/05/accessibility-selector) ::: ### Fetch by `aria-label` ```html reference https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/aria.html#L1 ``` ```js reference useHTTPS https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.js#L86-L87 ``` ### Fetch by `aria-labelledby` ```html reference https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/aria.html#L2-L3 ``` ```js reference useHTTPS https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.js#L93-L94 ``` ### Fetch by content ```html reference https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/aria.html#L4 ``` ```js reference useHTTPS https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.js#L100-L101 ``` ### Fetch by title ```html reference https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/aria.html#L5 ``` ```js reference useHTTPS https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.js#L107-L108 ``` ### Fetch by `alt` property ```html reference https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/aria.html#L6 ``` ```js reference useHTTPS https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.js#L114-L115 ``` ## ARIA - Role Attribute For querying elements based on [ARIA roles](https://www.w3.org/TR/html-aria/#docconformance), you can directly specify role of the element like `[role=button]` as selector parameter: ```html reference https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/aria.html#L13 ``` ```js reference useHTTPS https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.js#L131-L132 ``` ## ID Attribute Locator strategy "id" is not supported in WebDriver protocol, one should use either CSS or xPath selector strategies instead to find elements using ID. However some drivers (e.g. [Appium You.i Engine Driver](https://github.com/YOU-i-Labs/appium-youiengine-driver#selector-strategies)) might still [support](https://github.com/YOU-i-Labs/appium-youiengine-driver#selector-strategies) this selector. Current supported selector syntaxes for ID are: ```js //css locator const button = await $('#someid') //xpath locator const button = await $('//*[@id="someid"]') //id strategy // Note: works only in Appium or similar frameworks which supports locator strategy "ID" const button = await $('id=resource-id/iosname') ``` ## JS Function You can also use JavaScript functions to fetch elements using web native APIs. Of course, you can only do this inside a web context (e.g., `browser`, or web context in mobile). Given the following HTML structure: ```html reference https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/js.html ``` You can query the sibling element of `#elem` as follows: ```js reference useHTTPS https://github.com/webdriverio/example-recipes/blob/e8b147e88e7a38351b0918b4f7efbd9ae292201d/selectors/example.js#L139-L143 ``` ## Deep Selectors :::warning Starting with `v9` of WebdriverIO there is no need for this special selector as WebdriverIO automatically pierces through the Shadow DOM for you. It is recommended to migrate off this selector by removing the `>>>` in front it. ::: Many frontend applications heavily rely on elements with [shadow DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM). It is technically impossible to query elements within the shadow DOM without workarounds. The [`shadow$`](https://webdriver.io/docs/api/element/shadow$) and [`shadow$$`](https://webdriver.io/docs/api/element/shadow$$) have been such workarounds that had their [limitations](https://github.com/Georgegriff/query-selector-shadow-dom#how-is-this-different-to-shadow). With the deep selector you can now query all elements within any shadow DOM using the common query command. Given we have an application with the following structure: ![Chrome Example](https://github.com/Georgegriff/query-selector-shadow-dom/raw/main/Chrome-example.png "Chrome Example") With this selector you can query the `
``` And you want to add product B to the cart, it would be difficult to do that just by using the CSS selector. With selector chaining, it's way easier. Simply narrow down the desired element step by step: ```js await $('.row .entry:nth-child(2)').$('button*=Add').click() ``` ### Appium Image Selector Using the `-image` locator strategy, it is possible to send an Appium an image file representing an element you want to access. Supported file formats `jpg,png,gif,bmp,svg` Full reference can be found [here](https://github.com/appium/appium/blob/master/packages/images-plugin/docs/find-by-image.md) ```js const elem = await $('./file/path/of/image/test.jpg') await elem.click() ``` **Note**: The way how Appium works with this selector is that it will internally make a (app)screenshot and use the provided image selector to verify if the element can be found in that (app)screenshot. Be aware of the fact that Appium might resize the taken (app)screenshot to make it match the CSS-size of your (app)screen (this will happen on iPhones but also on Mac machines with a Retina display because the DPR is bigger than 1). This will result in not finding a match because the provided image selector might have been taken from the original screenshot. You can fix this by updating the Appium Server settings, see the [Appium docs](https://github.com/appium/appium/blob/master/packages/images-plugin/docs/find-by-image.md#related-settings) for the settings and [this comment](https://github.com/webdriverio/webdriverio/issues/6097#issuecomment-726675579) on a detailed explanation. ## React Selectors WebdriverIO provides a way to select React components based on the component name. To do this, you have a choice of two commands: `react$` and `react$$`. These commands allow you to select components off the [React VirtualDOM](https://reactjs.org/docs/faq-internals.html) and return either a single WebdriverIO Element or an array of elements (depending on which function is used). **Note**: The commands `react$` and `react$$` are similar in functionality, except that `react$$` will return *all* matching instances as an array of WebdriverIO elements, and `react$` will return the first found instance. #### Basic example ```jsx // index.jsx import React from 'react' import ReactDOM from 'react-dom' function MyComponent() { return (
MyComponent
) } function App() { return () } ReactDOM.render(, document.querySelector('#root')) ``` In the above code there is a simple `MyComponent` instance inside the application, which React is rendering inside a HTML element with `id="root"`. With the `browser.react$` command, you can select an instance of `MyComponent`: ```js const myCmp = await browser.react$('MyComponent') ``` Now that you have the WebdriverIO element stored in `myCmp` variable, you can execute element commands against it. #### Filtering components The library that WebdriverIO uses internally allows to filter your selection by props and/or state of the component. To do so, you need to pass a second argument for props and/or a third argument for state to the browser command. ```jsx // index.jsx import React from 'react' import ReactDOM from 'react-dom' function MyComponent(props) { return (
Hello { props.name || 'World' }!
) } function App() { return (
) } ReactDOM.render(, document.querySelector('#root')) ``` If you want to select the instance of `MyComponent` that has a prop `name` as `WebdriverIO`, you can execute the command like so: ```js const myCmp = await browser.react$('MyComponent', { props: { name: 'WebdriverIO' } }) ``` If you wanted to filter our selection by state, the `browser` command would looks something like so: ```js const myCmp = await browser.react$('MyComponent', { state: { myState: 'some value' } }) ``` #### Dealing with `React.Fragment` When using the `react$` command to select React [fragments](https://reactjs.org/docs/fragments.html), WebdriverIO will return the first child of that component as the component's node. If you use `react$$`, you will receive an array containing all the HTML nodes inside the fragments that match the selector. ```jsx // index.jsx import React from 'react' import ReactDOM from 'react-dom' function MyComponent() { return (
MyComponent
MyComponent
) } function App() { return () } ReactDOM.render(, document.querySelector('#root')) ``` Given the above example, this is how the commands would work: ```js await browser.react$('MyComponent') // returns the WebdriverIO Element for the first
await browser.react$$('MyComponent') // returns the WebdriverIO Elements for the array [
,
] ``` **Note:** If you have multiple instances of `MyComponent` and you use `react$$` to select these fragment components, you will be returned an one-dimensional array of all the nodes. In other words, if you have 3 `` instances, you will be returned an array with six WebdriverIO elements. ## Custom Selector Strategies If your app requires a specific way to fetch elements you can define yourself a custom selector strategy that you can use with `custom$` and `custom$$`. For that register your strategy once in the beginning of the test, e.g. in a `before` hook: ```js reference https://github.com/webdriverio/example-recipes/blob/f5730428ec3605e856e90bf58be17c9c9da891de/queryElements/customStrategy.js#L2-L11 ``` Given the following HTML snippet: ```html reference https://github.com/webdriverio/example-recipes/blob/f5730428ec3605e856e90bf58be17c9c9da891de/queryElements/example.html#L8-L12 ``` Then use it by calling: ```js reference https://github.com/webdriverio/example-recipes/blob/f5730428ec3605e856e90bf58be17c9c9da891de/queryElements/customStrategy.js#L16-L19 ``` **Note:** this only works in an web environment in which the [`execute`](/docs/api/browser/execute) command can be run.