MCPcopy Index your code
hub / github.com/SeleniumHQ/selenium / selectByIndex

Method selectByIndex

javascript/selenium-webdriver/lib/select.js:182–204  ·  view source on GitHub ↗

* * Select option with specified index. * * Option 1 Option 2 Option 3 const selectBox = await driver.findElement(By.id("selectbox")); await sele

(index)

Source from the content-addressed store, hash-verified

180 * @param index
181 */
182 async selectByIndex(index) {
183 if (index < 0) {
184 throw new Error('Index needs to be 0 or any other positive number')
185 }
186
187 let options = await this.element.findElements(By.tagName('option'))
188
189 if (options.length === 0) {
190 throw new Error("Select element doesn't contain any option element")
191 }
192
193 if (options.length - 1 < index) {
194 throw new Error(
195 `Option with index "${index}" not found. Select element only contains ${options.length - 1} option elements`,
196 )
197 }
198
199 for (let option of options) {
200 if ((await option.getAttribute('index')) === index.toString()) {
201 await this.setSelected(option)
202 }
203 }
204 }
205
206 /**
207 *

Callers

nothing calls this directly

Calls 5

setSelectedMethod · 0.95
tagNameMethod · 0.80
findElementsMethod · 0.65
getAttributeMethod · 0.65
toStringMethod · 0.65

Tested by

no test coverage detected