Skip to main content

selectByIndex

Overview

Use the selectByIndex command to select an option by a specific index.

Usage

await browser.$(selector).selectByIndex(index);

Command Parameters

NameTypeDescription
indexNumberThe index (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 value="someValue5">seis</option>
</select>

selectByIndex.js

it("should demonstrate the selectByIndex command", async ({ browser }) => {
const selectBox = await browser.$("#selectbox");
console.log(await selectBox.getValue()); // outputs: "someValue0"

await selectBox.selectByIndex(4);
console.log(await selectBox.getValue()); // outputs: "someValue4"
});

References

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