Skip to main content

selectByAttribute

Overview

Use the selectByAttribute command to select an option by a specific attribute value.

Usage

await browser.$(selector).selectByAttribute(attribute, value);

Command Parameters

NameTypeDescription
attributeStringThe attribute (of the option) by which to select the option.
valueString or NumberThe value (of the option) by which to select the option.

Usage Examples

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 ({ browser }) => {
const selectBox = await browser.$("#selectbox");
const value = await selectBox.getValue();
console.log(value); // outputs: "someValue0"

await selectBox.selectByAttribute("value", "someValue3");
console.log(await selectBox.getValue()); // outputs: "someValue3"

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

References

We'd like to give credit to the original WebdriverIO docs article, from which we drew some information while writing our version.