தேர்வாளர்கள்
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