$
$
命令是一种简短便捷的方式,用于在页面上获取单个元素。
您还可以传入一个对象作为选择器,该对象包含属性 element-6066-11e4-a52e-4f735466cecf
,
其值为元素的引用。然后,该命令将引用转换为扩展的 WebdriverIO 元素。
注意:链式使用 $
和 $$
命令只有在使用多个选择器策略时才有意义。否则,
您将发出不必要的请求,从而减慢测试速度(例如,$('body').$('div')
将触发两个请求,而
$('body div')
只用一个请求完成相同的操作)
您可以将 $
或 $$
链接在一起,无需将单个命令包装在 await
中,
以便遍历 DOM 树,例如:
const imageSrc = await $$('div')[1].nextElement().$$('img')[2].getAttribute('src')
WebdriverIO 在使用 $
或 $$
命令时无缝地遍历 shadow roots,无论嵌套级别或
shadow root 模式如何,例如:
await browser.url('https://ionicframework.com/docs/usage/v8/datetime/basic/demo.html?ionic:mode=md')
await browser.$('button[aria-label="Sunday, August 4"]').click()
await browser.$('.aux-input').getValue()
信息
有关如何选择特定元素的更多信息,请查看选择器指南。
用法
$(selector).$(selector)
参数
名称 | 类型 | 详情 |
---|---|---|
selector | String, Function, Matcher | 选择器、JS 函数或 Matcher 对象,用于获取特定元素 |
示例
example.html
loading...
singleElements.js
loading...
singleElements.js
loading...
singleElements.js
loading...
$.js
it('should use Androids DataMatcher or ViewMatcher selector', async () => {
const menuItem = await $({
"name": "hasEntry",
"args": ["title", "ViewTitle"],
"class": "androidx.test.espresso.matcher.ViewMatchers"
});
await menuItem.click();
const menuItem = await $({
"name": "hasEntry",
"args": ["title", "ViewTitle"]
});
await menuItem.click();
});
返回值
- <WebdriverIO.Element>