Trabalhando com elementos select
The Select object will now give you a series of commands
that allow you to interact with a <select>
element.
If you are using Java or .NET make sure that you’ve properly required the support package in your code. See the full code from GitHub in any of the examples below.
Note that this class only works for HTML elements select
and option
.
It is possible to design drop-downs with JavaScript overlays using div
or li
,
and this class will not work for those.
Types
Select methods may behave differently depending on which type of <select>
element is being worked with.
Single select
This is the standard drop-down object where one and only one option may be selected.
Multiple select
This select list allows selecting and deselecting more than one option at a time.
This only applies to <select>
elements with the multiple
attribute.
Create class
First locate a <select>
element, then use it to initialize a Select
object.
Note that as of Selenium 4.5, you can’t create a Select
object if the <select>
element is disabled.
List options
There are two lists that can be obtained:
All options
Get a list of all options in the <select>
element:
Selected options
Get a list of selected options in the <select>
element. For a standard select list
this will only be a list with one element, for a multiple select list it can contain
zero or many elements.
Select option
The Select class provides three ways to select an option. Note that for multiple select type Select lists, you can repeat these methods for each element you want to select.
Text
Select the option based on its visible text
Value
Select the option based on its value attribute
Index
Select the option based on its position in the list
Disabled options
Options with a disabled
attribute may not be selected.
De-select option
Only multiple select type select lists can have options de-selected. You can repeat these methods for each element you want to select.