தேர்வாளர்கள்
The WebDriver Protocol 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.
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:
<button
id="main"
class="btn btn-large"
name="submission"
role="button"
data-testid="submit"
>
Submit
</button>
We do and do not recommend the following selectors:
| தேர்வாளர் | பரிந்துரைக்கப்படுகிறது | குறிப்புகள் |
|---|---|---|
$('button') | 🚨 ஒருபோதும் இல்லை | மோசமானது - மிகவும் பொதுவானது, சூழல் இல்லை. |
$('.btn.btn-large') | 🚨 ஒருபோதும் இல்லை | மோசம். ஸ்டைலிங்குடன் இணைந்துள்ளது. மாறுதலுக்கு மிகவும் உட்பட்டது. |
$('#main') | ⚠️ சிறிதளவு | சிறப்பானது. ஆனால் இன்னும் ஸ்டைலிங் அல்லது JS நிகழ்வு கேட்பவர்களுடன் இணைக்கப்பட்டுள்ளது. |
$(() => document.queryElement('button')) | ⚠️ சிறிதளவு | திறம்பட வினவுகிறது, எழுத சிக்கலானது. |
$('button[name="submission"]') | ⚠️ சிறிதளவு | HTML சொற்பொருள் கொண்ட name பண்புக்கூறுடன் இணைக்கப்பட்டுள்ளது. |
$('button[data-testid="submit"]') | ✅ நல்லது | கூடுதல் பண்புக்கூறு தேவை, a11y உடன் இணைக்கப்படவில்லை. |
$('aria/Submit') | ✅ நல்லது | நல்லது. பயனர் பக்கத்துடன் எவ்வாறு தொடர்புகொள்கிறார் என்பதைப் போன்றது. மொழிபெயர்ப்புகள் புதுப்பிக்கப்படும்போது உங்கள் சோதனைகள் உடைந்து போகாமல் இருக்க மொழிபெயர்ப்பு கோப்புகளைப் பயன்படுத்த பரிந்துரைக்கப்படுகிறது. குறிப்பு: பெரிய பக்கங்களில் இந்த தேர்வாளர் மற்றவற்றைவிட மெதுவாக இருக்கலாம். |
$('button=Submit') | ✅ எப்போதும் | சிறந்தது. பயனர் பக்கத்துடன் எவ்வாறு தொடர்புகொள்கிறார் என்பதைப் போன்றது மற்றும் விரைவானது. மொழிபெயர்ப்புகள் புதுப்பிக்கப்படும்போது உங்கள் சோதனைகள் உடைந்து போகாமல் இருக்க மொழிபெயர்ப்பு கோப்புகளைப் பயன்படுத்த பரிந்துரைக்கப்படுகிறது. |
CSS Query Selector
If not indicated otherwise, WebdriverIO will query elements using the CSS selector pattern, e.g.:
loading...
Link Text
To get an anchor element with a specific text in it, query the text starting with an equals (=) sign.
For example:
loading...
You can query this element by calling:
loading...
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:
loading...
Note: You can't mix multiple selector strategies in one selector. Use multiple chained element queries to reach the same goal, e.g.:
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":
loading...
You can query this element by calling:
loading...
Or using query partial text:
loading...
The same works for id and class names:
loading...
You can query this element by calling:
loading...
Note: You can't mix multiple selector strategies in one selector. Use multiple chained element queries to reach the same goal, e.g.:
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 <tag> or <tag />.
loading...
You can query this element by calling:
loading...
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 by passing something like [name="some-name"] as selector parameter:
loading...
loading...
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.
An xPath selector has a format like //body/div[6]/div[1]/span[1].
loading...
You can query the second paragraph by calling:
loading...
You can use xPath to also traverse up and down the DOM tree:
loading...
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.
You can read more about this selector in our release blog post
Fetch by aria-label
loading...
loading...
Fetch by aria-labelledby
loading...
loading...
Fetch by content
loading...
loading...
Fetch by title
loading...
loading...
Fetch by alt property
loading...
loading...
ARIA - Role Attribute
For querying elements based on ARIA roles, you can directly specify role of the element like [role=button] as selector parameter:
loading...
loading...
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) might still support this selector.
Current supported selector syntaxes for ID are:
//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:
loading...
You can query the sibling element of #elem as follows:
loading...
Deep Selectors
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. It is technically impossible to query elements within the shadow DOM without workarounds. The shadow$ and shadow$$ have been such workarounds that had their limitations. 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:

With this selector you can query the <button /> element that is nested within another shadow DOM, e.g.:
loading...
Mobile Selectors
For hybrid mobile testing, it's important that the automation server is in the correct context before executing commands. For automating gestures, the driver ideally should be set to native context. But to select elements from the DOM, the driver will need to be set to the platform's webview context. Only then can the methods mentioned above can be used.
For native mobile testing, there is no switching between contexts, as you have to use mobile strategies and use the underlying device automation technology directly. This is especially useful when a test needs some fine-grained control over finding elements.
Android UiAutomator
Android's UI Automator framework provides a number of ways to find elements. You can use the UI Automator API, in particular the UiSelector class to locate elements. In Appium you send the Java code, as a string, to the server, which executes it in the application's environment, returning the element or elements.
const selector = 'new UiSelector().text("Cancel").className("android.widget.Button")'
const button = await $(`android=${selector}`)
await button.click()