跳至主要內容

selectByAttribute

選取具有特定值的選項。

用法
$(selector).selectByAttribute(attribute, value)
參數
名稱類型詳細資訊
attribute字串要選取選項元素的屬性
value字串數字要選取選項元素的值
範例
example.html
<select id="selectbox">
<option value="someValue0">uno</option>
<option value="someValue1">dos</option>
<option value="someValue2">tres</option>
<option value="someValue3">cuatro</option>
<option value="someValue4">cinco</option>
<option name="someName5" value="someValue5">seis</option>
</select>
selectByAttribute.js
it('Should demonstrate the selectByAttribute command', async () => {
const selectBox = await $('#selectbox');
const value = await selectBox.getValue();
console.log(value); // returns "someValue0"

await selectBox.selectByAttribute('value', 'someValue3');
console.log(await selectBox.getValue()); // returns "someValue3"

await selectBox.selectByAttribute('name', 'someName5');
console.log(await selectBox.getValue()); // returns "someValue5"
});

歡迎!我能如何協助您?

WebdriverIO AI Copilot