跳至主要內容

$$

$$ 命令是一種簡短方便的方式,用於在頁面上獲取多個元素。它會回傳一個包含一組 WebdriverIO 元素的 ChainablePromiseArray

資訊

與附加在 瀏覽器物件上的 $$ 不同,此命令會根據根元素查詢元素。

您可以將 $$$ 連結在一起,而無需將個別命令包裝在 await 中,以便向下瀏覽 DOM 樹,例如:

const imageSrc = await $$('div')[1].nextElement().$$('img')[2].getAttribute('src')

當使用 $$$ 命令時,無論巢狀層級或陰影根模式為何,WebdriverIO 都能無縫地遍歷陰影根,例如

await browser.url('https://ionic.dev.org.tw/docs/usage/v8/datetime/basic/demo.html?ionic:mode=md')
await browser.$('button[aria-label="Sunday, August 4"]').click()
await browser.$('.aux-input').getValue()

也可以使用非同步迭代器來迴圈處理查詢結果,例如:

// print all image sources
for await (const img of $$('img')) {
console.log(await img.getAttribute('src'))
}
資訊

如需更多關於如何選取特定元素的資訊,請查看選取器指南。

用法
$(selector).$$(selector)
參數
名稱類型詳細資訊
selectorStringFunctionMatcher用於獲取多個元素的選取器、JS 函式或 Matcher 物件
範例
example.html
loading...
multipleElements.js
loading...
multipleElements.js
loading...
multipleElements.js
loading...

歡迎!我能幫你什麼嗎?

WebdriverIO AI Copilot