select_by_index
The command to select an <option> by its index within a <select> dropdown element.
Syntax
Element.select_by_index(index: int) -> Element
Usage
py.get("#dropdown").select_by_index(2)
---or--- # chain an Element command
py.get("#dropdown").select_by_index(0).get_attribute("value")
# Errors, can only perform this command on a <select> dropdown element
py.get("ul > li").select_by_index(1)
Arguments
index (int)
- The index or "position" of the option to select.
Yields
Element - The current instance of Element so you can chain commands.
Examples
Given this HTML
<select id="dropdown">
<option value="" disabled="disabled" selected="selected">Please select an option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
We can select any of the options
dropdown = py.get("dropdown")
# Select the first option that is "disabled"
dropdown.select_by_index(0)
# Select Option 1
dropdown.select_by_index(1)
# Select Option 2
dropdown.select_by_index(2)
See also
click() - If the dropdown is NOT a <select> element,
.click()
will work
Last updated