The core libraries of Selenium try to be low level and non-opinionated. The Support classes in each language provide opinionated wrappers for common interactions that may be used to simplify some behaviors.
This is the multi-page printable view of this section. Click here to print.
Support features
- 1: Waiting with Expected Conditions
- 2: Command Listeners
- 3: Working With Colors
- 4: Working with select list elements
- 5: ThreadGuard
1 - Waiting with Expected Conditions
Expected Conditions are used with Explicit Waits. Instead of defining the block of code to be executed with a lambda, an expected conditions method can be created to represent common things that get waited on. Some methods take locators as arguments, others take elements as arguments.
These methods can include conditions such as:
- element exists
- element is stale
- element is visible
- text is visible
- title contains specified value
2 - Command Listeners
These allow you to execute custom actions in every time specific Selenium commands are sent
3 - Working With Colors
You will occasionally want to validate the colour of something as part of your tests; the problem is that colour definitions on the web are not constant. Would it not be nice if there was an easy way to compare a HEX representation of a colour with a RGB representation of a colour, or a RGBA representation of a colour with a HSLA representation of a colour?
Worry not. There is a solution: the Color class!
First of all, you will need to import the class:
import org.openqa.selenium.support.Color;
from selenium.webdriver.support.color import Color
include Selenium::WebDriver::Support
import org.openqa.selenium.support.Color
You can now start creating colour objects. Every colour object will need to be created from a string representation of your colour. Supported colour representations are:
private final Color HEX_COLOUR = Color.fromString("#2F7ED8");
private final Color RGB_COLOUR = Color.fromString("rgb(255, 255, 255)");
private final Color RGB_COLOUR = Color.fromString("rgb(40%, 20%, 40%)");
private final Color RGBA_COLOUR = Color.fromString("rgba(255, 255, 255, 0.5)");
private final Color RGBA_COLOUR = Color.fromString("rgba(40%, 20%, 40%, 0.5)");
private final Color HSL_COLOUR = Color.fromString("hsl(100, 0%, 50%)");
private final Color HSLA_COLOUR = Color.fromString("hsla(100, 0%, 50%, 0.5)");
HEX_COLOUR = Color.from_string('#2F7ED8')
RGB_COLOUR = Color.from_string('rgb(255, 255, 255)')
RGB_COLOUR = Color.from_string('rgb(40%, 20%, 40%)')
RGBA_COLOUR = Color.from_string('rgba(255, 255, 255, 0.5)')
RGBA_COLOUR = Color.from_string('rgba(40%, 20%, 40%, 0.5)')
HSL_COLOUR = Color.from_string('hsl(100, 0%, 50%)')
HSLA_COLOUR = Color.from_string('hsla(100, 0%, 50%, 0.5)')
HEX_COLOUR = Color.from_string('#2F7ED8')
RGB_COLOUR = Color.from_string('rgb(255, 255, 255)')
RGB_COLOUR = Color.from_string('rgb(40%, 20%, 40%)')
RGBA_COLOUR = Color.from_string('rgba(255, 255, 255, 0.5)')
RGBA_COLOUR = Color.from_string('rgba(40%, 20%, 40%, 0.5)')
HSL_COLOUR = Color.from_string('hsl(100, 0%, 50%)')
HSLA_COLOUR = Color.from_string('hsla(100, 0%, 50%, 0.5)')
private val HEX_COLOUR = Color.fromString("#2F7ED8")
private val RGB_COLOUR = Color.fromString("rgb(255, 255, 255)")
private val RGB_COLOUR_PERCENT = Color.fromString("rgb(40%, 20%, 40%)")
private val RGBA_COLOUR = Color.fromString("rgba(255, 255, 255, 0.5)")
private val RGBA_COLOUR_PERCENT = Color.fromString("rgba(40%, 20%, 40%, 0.5)")
private val HSL_COLOUR = Color.fromString("hsl(100, 0%, 50%)")
private val HSLA_COLOUR = Color.fromString("hsla(100, 0%, 50%, 0.5)")
The Color class also supports all of the base colour definitions specified in http://www.w3.org/TR/css3-color/#html4.
private final Color BLACK = Color.fromString("black");
private final Color CHOCOLATE = Color.fromString("chocolate");
private final Color HOTPINK = Color.fromString("hotpink");
BLACK = Color.from_string('black')
CHOCOLATE = Color.from_string('chocolate')
HOTPINK = Color.from_string('hotpink')
BLACK = Color.from_string('black')
CHOCOLATE = Color.from_string('chocolate')
HOTPINK = Color.from_string('hotpink')
private val BLACK = Color.fromString("black")
private val CHOCOLATE = Color.fromString("chocolate")
private val HOTPINK = Color.fromString("hotpink")
Sometimes browsers will return a colour value of “transparent” if no colour has been set on an element. The Color class also supports this:
private final Color TRANSPARENT = Color.fromString("transparent");
TRANSPARENT = Color.from_string('transparent')
TRANSPARENT = Color.from_string('transparent')
private val TRANSPARENT = Color.fromString("transparent")
You can now safely query an element to get its colour/background colour knowing that any response will be correctly parsed and converted into a valid Color object:
Color loginButtonColour = Color.fromString(driver.findElement(By.id("login")).getCssValue("color"));
Color loginButtonBackgroundColour = Color.fromString(driver.findElement(By.id("login")).getCssValue("background-color"));
login_button_colour = Color.from_string(driver.find_element(By.ID,'login').value_of_css_property('color'))
login_button_background_colour = Color.from_string(driver.find_element(By.ID,'login').value_of_css_property('background-color'))
login_button_colour = Color.from_string(driver.find_element(id: 'login').css_value('color'))
login_button_background_colour = Color.from_string(driver.find_element(id: 'login').css_value('background-color'))
val loginButtonColour = Color.fromString(driver.findElement(By.id("login")).getCssValue("color"))
val loginButtonBackgroundColour = Color.fromString(driver.findElement(By.id("login")).getCssValue("background-color"))
You can then directly compare colour objects:
assert loginButtonBackgroundColour.equals(HOTPINK);
assert login_button_background_colour == HOTPINK
assert(login_button_background_colour == HOTPINK)
assert(loginButtonBackgroundColour.equals(HOTPINK))
Or you can convert the colour into one of the following formats and perform a static validation:
assert loginButtonBackgroundColour.asHex().equals("#ff69b4");
assert loginButtonBackgroundColour.asRgba().equals("rgba(255, 105, 180, 1)");
assert loginButtonBackgroundColour.asRgb().equals("rgb(255, 105, 180)");
assert login_button_background_colour.hex == '#ff69b4'
assert login_button_background_colour.rgba == 'rgba(255, 105, 180, 1)'
assert login_button_background_colour.rgb == 'rgb(255, 105, 180)'
assert(login_button_background_colour.hex == '#ff69b4')
assert(login_button_background_colour.rgba == 'rgba(255, 105, 180, 1)')
assert(login_button_background_colour.rgb == 'rgb(255, 105, 180)')
assert(loginButtonBackgroundColour.asHex().equals("#ff69b4"))
assert(loginButtonBackgroundColour.asRgba().equals("rgba(255, 105, 180, 1)"))
assert(loginButtonBackgroundColour.asRgb().equals("rgb(255, 105, 180)"))
Colours are no longer a problem.
4 - Working with select list elements
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.
<select name="selectomatic">
<option selected="selected" id="non_multi_option" value="one">One</option>
<option value="two">Two</option>
<option value="four">Four</option>
<option value="still learning how to count, apparently">Still learning how to count, apparently</option>
</select>
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.
<select name="multi" id="multi" multiple="multiple">
<option selected="selected" value="eggs">Eggs</option>
<option value="ham">Ham</option>
<option selected="selected" value="sausages">Sausages</option>
<option value="onion gravy">Onion gravy</option>
</select>
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.
WebElement selectElement = driver.findElement(By.name("selectomatic"));
Select select = new Select(selectElement);
Show full example
package dev.selenium.support;
import dev.selenium.BaseChromeTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import java.util.ArrayList;
import java.util.List;
public class SelectListTest extends BaseChromeTest {
@BeforeEach
public void navigate() {
driver.get("https://www.selenium.dev/selenium/web/formPage.html");
}
@Test
public void selectOption() {
WebElement selectElement = driver.findElement(By.name("selectomatic"));
Select select = new Select(selectElement);
WebElement twoElement = driver.findElement(By.cssSelector("option[value=two]"));
WebElement fourElement = driver.findElement(By.cssSelector("option[value=four]"));
WebElement countElement = driver.findElement(By.cssSelector("option[value='still learning how to count, apparently']"));
select.selectByVisibleText("Four");
Assertions.assertTrue(fourElement.isSelected());
select.selectByValue("two");
Assertions.assertTrue(twoElement.isSelected());
select.selectByIndex(3);
Assertions.assertTrue(countElement.isSelected());
}
@Test
public void selectMultipleOption() {
WebElement selectElement = driver.findElement(By.name("multi"));
Select select = new Select(selectElement);
WebElement hamElement = driver.findElement(By.cssSelector("option[value=ham]"));
WebElement gravyElement = driver.findElement(By.cssSelector("option[value='onion gravy']"));
WebElement eggElement = driver.findElement(By.cssSelector("option[value=eggs]"));
WebElement sausageElement = driver.findElement(By.cssSelector("option[value='sausages']"));
List<WebElement> optionElements = selectElement.findElements(By.tagName("option"));
List<WebElement> optionList = select.getOptions();
Assertions.assertEquals(optionElements, optionList);
List<WebElement> selectedOptionList = select.getAllSelectedOptions();
List<WebElement> expectedSelection = new ArrayList<WebElement>() {{
add(eggElement);
add(sausageElement);
}};
Assertions.assertEquals(expectedSelection, selectedOptionList);
select.selectByValue("ham");
select.selectByValue("onion gravy");
Assertions.assertTrue(hamElement.isSelected());
Assertions.assertTrue(gravyElement.isSelected());
select.deselectByValue("eggs");
select.deselectByValue("sausages");
Assertions.assertFalse(eggElement.isSelected());
Assertions.assertFalse(sausageElement.isSelected());
}
@Test
public void disabledOption() {
WebElement selectElement = driver.findElement(By.name("single_disabled"));
Select select = new Select(selectElement);
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
select.selectByValue("disabled");
});
}
}
select_element = driver.find_element(By.NAME, 'selectomatic')
select = Select(select_element)
Show full example
import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
def test_select_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'selectomatic')
select = Select(select_element)
two_element = driver.find_element(By.CSS_SELECTOR, 'option[value=two]')
four_element = driver.find_element(By.CSS_SELECTOR, 'option[value=four]')
count_element = driver.find_element(By.CSS_SELECTOR, "option[value='still learning how to count, apparently']")
select.select_by_visible_text('Four')
assert four_element.is_selected()
select.select_by_value('two')
assert two_element.is_selected()
select.select_by_index(3)
assert count_element.is_selected()
def test_select_multiple_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'multi')
select = Select(select_element)
ham_element = driver.find_element(By.CSS_SELECTOR, 'option[value=ham]')
gravy_element = driver.find_element(By.CSS_SELECTOR, "option[value='onion gravy']")
egg_element = driver.find_element(By.CSS_SELECTOR, 'option[value=eggs]')
sausage_element = driver.find_element(By.CSS_SELECTOR, "option[value='sausages']")
option_elements = select_element.find_elements(By.TAG_NAME, 'option')
option_list = select.options
assert option_elements == option_list
selected_option_list = select.all_selected_options
expected_selection = [egg_element, sausage_element]
assert selected_option_list == expected_selection
select.select_by_value('ham')
select.select_by_value('onion gravy')
assert ham_element.is_selected()
assert gravy_element.is_selected()
select.deselect_by_value('eggs')
select.deselect_by_value('sausages')
assert not egg_element.is_selected()
assert not sausage_element.is_selected()
def test_disabled_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'single_disabled')
select = Select(select_element)
with pytest.raises(NotImplementedError):
select.select_by_value('disabled')
var selectElement = driver.FindElement(By.Name("selectomatic"));
var select = new SelectElement(selectElement);
Show full example
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace SeleniumDocs.Support
{
[TestClass]
public class SelectListTest : BaseChromeTest
{
[TestInitialize]
public void Navigate()
{
driver.Url = "https://www.selenium.dev/selenium/web/formPage.html";
}
[TestMethod]
public void SelectOption()
{
var selectElement = driver.FindElement(By.Name("selectomatic"));
var select = new SelectElement(selectElement);
var twoElement = driver.FindElement(By.CssSelector("option[value=two]"));
var fourElement = driver.FindElement(By.CssSelector("option[value=four]"));
var countElement = driver.FindElement(By.CssSelector("option[value='still learning how to count, apparently']"));
select.SelectByText("Four");
Assert.IsTrue(fourElement.Selected);
select.SelectByValue("two");
Assert.IsTrue(twoElement.Selected);
select.SelectByIndex(3);
Assert.IsTrue(countElement.Selected);
}
[TestMethod]
public void SelectMultipleOption()
{
var selectElement = driver.FindElement(By.Name("multi"));
var select = new SelectElement(selectElement);
var hamElement = driver.FindElement(By.CssSelector("option[value=ham]"));
var gravyElement = driver.FindElement(By.CssSelector("option[value='onion gravy']"));
var eggElement = driver.FindElement(By.CssSelector("option[value=eggs]"));
var sausageElement = driver.FindElement(By.CssSelector("option[value='sausages']"));
IList<IWebElement> optionList = select.Options;
IWebElement[] optionElements = selectElement.FindElements(By.TagName("option")).ToArray();
CollectionAssert.AreEqual(optionElements, optionList.ToArray());
IList<IWebElement> selectedOptionList = select.AllSelectedOptions;
IWebElement[] expectedSelection = { eggElement, sausageElement };
CollectionAssert.AreEqual(expectedSelection, selectedOptionList.ToArray());
select.SelectByValue("ham");
select.SelectByValue("onion gravy");
Assert.IsTrue(hamElement.Selected);
Assert.IsTrue(gravyElement.Selected);
select.DeselectByValue("eggs");
select.DeselectByValue("sausages");
Assert.IsFalse(eggElement.Selected);
Assert.IsFalse(sausageElement.Selected);
}
[TestMethod]
public void DisabledOption()
{
var selectElement = driver.FindElement(By.Name("single_disabled"));
var select = new SelectElement(selectElement);
Assert.ThrowsException<InvalidOperationException>(() => select.SelectByValue("disabled"));
}
}
}
select_element = driver.find_element(name: 'selectomatic')
select = Selenium::WebDriver::Support::Select.new(select_element)
Show full example
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Select List' do
let(:driver) { start_session }
before do
driver.get('https://www.selenium.dev/selenium/web/formPage.html')
end
it 'select options' do
select_element = driver.find_element(name: 'selectomatic')
select = Selenium::WebDriver::Support::Select.new(select_element)
two_element = driver.find_element(css: 'option[value=two]')
four_element = driver.find_element(css: 'option[value=four]')
count_element = driver.find_element(css: "option[value='still learning how to count, apparently']")
select.select_by(:text, 'Four')
expect(four_element).to be_selected
select.select_by(:value, 'two')
expect(two_element).to be_selected
select.select_by(:index, 3)
expect(count_element).to be_selected
end
it 'select multiple options' do
select_element = driver.find_element(name: 'multi')
select = Selenium::WebDriver::Support::Select.new(select_element)
ham_element = driver.find_element(css: 'option[value=ham]')
gravy_element = driver.find_element(css: "option[value='onion gravy']")
egg_element = driver.find_element(css: 'option[value=eggs]')
sausage_element = driver.find_element(css: "option[value='sausages']")
option_elements = select_element.find_elements(tag_name: 'option')
option_list = select.options
expect(option_elements).to eq option_list
selected_option_list = select.selected_options
expected_selection = [egg_element, sausage_element]
expect(selected_option_list).to eq expected_selection
select.select_by(:value, 'ham')
select.select_by(:value, 'onion gravy')
expect(ham_element).to be_selected
expect(gravy_element).to be_selected
select.deselect_by(:value, 'eggs')
select.deselect_by(:value, 'sausages')
expect(egg_element).not_to be_selected
expect(sausage_element).not_to be_selected
end
it 'disabled options' do
select_element = driver.find_element(name: 'single_disabled')
select = Selenium::WebDriver::Support::Select.new(select_element)
expect {
select.select_by(:value, 'disabled')
}.to raise_exception(Selenium::WebDriver::Error::UnsupportedOperationError)
end
end
it('Select an option', async function () {
Show full example
const {By, Browser, Builder} = require('selenium-webdriver')
const assert = require('assert/strict')
const {Select} = require('selenium-webdriver')
describe('Select Tests', async function () {
let driver
before(async function () {
driver = new Builder()
.forBrowser(Browser.FIREFOX)
.build()
await driver.get('https://www.selenium.dev/selenium/web/formPage.html')
})
after(async () => await driver.quit())
it('Select an option', async function () {
const selectElement = await driver.findElement(By.name('selectomatic'))
const select = new Select(selectElement)
const twoElement = await driver.findElement(By.css('option[value=two]'))
const fourElement = await driver.findElement(By.css('option[value=four]'))
const countElement = await driver.findElement(By.css("option[value='still learning how to count, apparently']"))
await select.selectByVisibleText('Four')
assert.equal(true, await fourElement.isSelected())
await select.selectByValue('two')
assert.equal(true, await twoElement.isSelected())
await select.selectByIndex(3)
assert.equal(true, await countElement.isSelected())
})
it('Select by multiple options', async function () {
const selectElement = await driver.findElement(By.name('multi'))
const select = await new Select(selectElement)
const hamElement = await driver.findElement(By.css('option[value=ham]'))
const gravyElement = await driver.findElement(By.css("option[value='onion gravy']"))
const eggElement = await driver.findElement(By.css('option[value=eggs]'))
const sausageElement = await driver.findElement(By.css("option[value='sausages']"))
const optionElements = await selectElement.findElements(By.css('option'))
const optionList = await select.getOptions()
assert.equal(optionList.length, optionElements.length)
for (const index in optionList) {
assert.equal(await optionList[index].getText(), await optionElements[index].getText())
}
const selectedOptionList = await select.getAllSelectedOptions()
const expectedSelection = [eggElement, sausageElement]
assert.equal(expectedSelection.length, selectedOptionList.length)
for (const index in selectedOptionList) {
assert.equal(await selectedOptionList[index].getText(), await expectedSelection[index].getText())
}
await select.selectByValue('ham')
await select.selectByValue('onion gravy')
assert.equal(true, await hamElement.isSelected())
assert.equal(true, await gravyElement.isSelected())
await select.deselectByValue('eggs')
await select.deselectByValue('sausages')
assert.equal(false, await eggElement.isSelected())
assert.equal(false, await sausageElement.isSelected())
})
it('Try selecting disabled option', async function () {
const selectElement = await driver.findElement(By.name('single_disabled'))
const select = await new Select(selectElement)
await assert.rejects(async () => {
await select.selectByValue("disabled")
}, {
name: 'UnsupportedOperationError',
message: 'You may not select a disabled option'
})
})
})
val selectElement = driver.findElement(By.name("selectomatic"))
val select = Select(selectElement)
Show full example
package dev.selenium.support
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.ui.Select
class SelectListTest : BaseTest() {
@BeforeEach
fun navigate() {
driver.get("https://www.selenium.dev/selenium/web/formPage.html")
}
@Test
fun selectOption() {
val selectElement = driver.findElement(By.name("selectomatic"))
val select = Select(selectElement)
val twoElement = driver.findElement(By.cssSelector("option[value=two]"))
val fourElement = driver.findElement(By.cssSelector("option[value=four]"))
val countElement = driver.findElement(By.cssSelector("option[value='still learning how to count, apparently']"))
select.selectByVisibleText("Four")
Assertions.assertTrue(fourElement.isSelected())
select.selectByValue("two")
Assertions.assertTrue(twoElement.isSelected())
select.selectByIndex(3)
Assertions.assertTrue(countElement.isSelected())
}
@Test
fun selectMultipleOption() {
val selectElement = driver.findElement(By.name("multi"))
val select = Select(selectElement)
val hamElement = driver.findElement(By.cssSelector("option[value=ham]"))
val gravyElement = driver.findElement(By.cssSelector("option[value='onion gravy']"))
val eggElement = driver.findElement(By.cssSelector("option[value=eggs]"))
val sausageElement = driver.findElement(By.cssSelector("option[value='sausages']"))
val optionElements = selectElement.findElements(By.tagName("option"))
val optionList = select.getOptions()
Assertions.assertEquals(optionElements, optionList)
val selectedOptionList = select.getAllSelectedOptions()
val expectedSelection = ArrayList<WebElement>()
expectedSelection.add(eggElement)
expectedSelection.add(sausageElement)
Assertions.assertEquals(expectedSelection, selectedOptionList)
select.selectByValue("ham")
select.selectByValue("onion gravy")
Assertions.assertTrue(hamElement.isSelected())
Assertions.assertTrue(gravyElement.isSelected())
select.deselectByValue("eggs")
select.deselectByValue("sausages")
Assertions.assertFalse(eggElement.isSelected())
Assertions.assertFalse(sausageElement.isSelected())
}
@Test
fun disabledOption() {
val selectElement = driver.findElement(By.name("single_disabled"))
val select = Select(selectElement)
Assertions.assertThrows(UnsupportedOperationException::class.java) {
select.selectByValue("disabled")
}
}
}
List options
There are two lists that can be obtained:
All options
Get a list of all options in the <select>
element:
List<WebElement> optionList = select.getOptions();
Show full example
package dev.selenium.support;
import dev.selenium.BaseChromeTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import java.util.ArrayList;
import java.util.List;
public class SelectListTest extends BaseChromeTest {
@BeforeEach
public void navigate() {
driver.get("https://www.selenium.dev/selenium/web/formPage.html");
}
@Test
public void selectOption() {
WebElement selectElement = driver.findElement(By.name("selectomatic"));
Select select = new Select(selectElement);
WebElement twoElement = driver.findElement(By.cssSelector("option[value=two]"));
WebElement fourElement = driver.findElement(By.cssSelector("option[value=four]"));
WebElement countElement = driver.findElement(By.cssSelector("option[value='still learning how to count, apparently']"));
select.selectByVisibleText("Four");
Assertions.assertTrue(fourElement.isSelected());
select.selectByValue("two");
Assertions.assertTrue(twoElement.isSelected());
select.selectByIndex(3);
Assertions.assertTrue(countElement.isSelected());
}
@Test
public void selectMultipleOption() {
WebElement selectElement = driver.findElement(By.name("multi"));
Select select = new Select(selectElement);
WebElement hamElement = driver.findElement(By.cssSelector("option[value=ham]"));
WebElement gravyElement = driver.findElement(By.cssSelector("option[value='onion gravy']"));
WebElement eggElement = driver.findElement(By.cssSelector("option[value=eggs]"));
WebElement sausageElement = driver.findElement(By.cssSelector("option[value='sausages']"));
List<WebElement> optionElements = selectElement.findElements(By.tagName("option"));
List<WebElement> optionList = select.getOptions();
Assertions.assertEquals(optionElements, optionList);
List<WebElement> selectedOptionList = select.getAllSelectedOptions();
List<WebElement> expectedSelection = new ArrayList<WebElement>() {{
add(eggElement);
add(sausageElement);
}};
Assertions.assertEquals(expectedSelection, selectedOptionList);
select.selectByValue("ham");
select.selectByValue("onion gravy");
Assertions.assertTrue(hamElement.isSelected());
Assertions.assertTrue(gravyElement.isSelected());
select.deselectByValue("eggs");
select.deselectByValue("sausages");
Assertions.assertFalse(eggElement.isSelected());
Assertions.assertFalse(sausageElement.isSelected());
}
@Test
public void disabledOption() {
WebElement selectElement = driver.findElement(By.name("single_disabled"));
Select select = new Select(selectElement);
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
select.selectByValue("disabled");
});
}
}
option_list = select.options
Show full example
import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
def test_select_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'selectomatic')
select = Select(select_element)
two_element = driver.find_element(By.CSS_SELECTOR, 'option[value=two]')
four_element = driver.find_element(By.CSS_SELECTOR, 'option[value=four]')
count_element = driver.find_element(By.CSS_SELECTOR, "option[value='still learning how to count, apparently']")
select.select_by_visible_text('Four')
assert four_element.is_selected()
select.select_by_value('two')
assert two_element.is_selected()
select.select_by_index(3)
assert count_element.is_selected()
def test_select_multiple_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'multi')
select = Select(select_element)
ham_element = driver.find_element(By.CSS_SELECTOR, 'option[value=ham]')
gravy_element = driver.find_element(By.CSS_SELECTOR, "option[value='onion gravy']")
egg_element = driver.find_element(By.CSS_SELECTOR, 'option[value=eggs]')
sausage_element = driver.find_element(By.CSS_SELECTOR, "option[value='sausages']")
option_elements = select_element.find_elements(By.TAG_NAME, 'option')
option_list = select.options
assert option_elements == option_list
selected_option_list = select.all_selected_options
expected_selection = [egg_element, sausage_element]
assert selected_option_list == expected_selection
select.select_by_value('ham')
select.select_by_value('onion gravy')
assert ham_element.is_selected()
assert gravy_element.is_selected()
select.deselect_by_value('eggs')
select.deselect_by_value('sausages')
assert not egg_element.is_selected()
assert not sausage_element.is_selected()
def test_disabled_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'single_disabled')
select = Select(select_element)
with pytest.raises(NotImplementedError):
select.select_by_value('disabled')
IList<IWebElement> optionList = select.Options;
Show full example
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace SeleniumDocs.Support
{
[TestClass]
public class SelectListTest : BaseChromeTest
{
[TestInitialize]
public void Navigate()
{
driver.Url = "https://www.selenium.dev/selenium/web/formPage.html";
}
[TestMethod]
public void SelectOption()
{
var selectElement = driver.FindElement(By.Name("selectomatic"));
var select = new SelectElement(selectElement);
var twoElement = driver.FindElement(By.CssSelector("option[value=two]"));
var fourElement = driver.FindElement(By.CssSelector("option[value=four]"));
var countElement = driver.FindElement(By.CssSelector("option[value='still learning how to count, apparently']"));
select.SelectByText("Four");
Assert.IsTrue(fourElement.Selected);
select.SelectByValue("two");
Assert.IsTrue(twoElement.Selected);
select.SelectByIndex(3);
Assert.IsTrue(countElement.Selected);
}
[TestMethod]
public void SelectMultipleOption()
{
var selectElement = driver.FindElement(By.Name("multi"));
var select = new SelectElement(selectElement);
var hamElement = driver.FindElement(By.CssSelector("option[value=ham]"));
var gravyElement = driver.FindElement(By.CssSelector("option[value='onion gravy']"));
var eggElement = driver.FindElement(By.CssSelector("option[value=eggs]"));
var sausageElement = driver.FindElement(By.CssSelector("option[value='sausages']"));
IList<IWebElement> optionList = select.Options;
IWebElement[] optionElements = selectElement.FindElements(By.TagName("option")).ToArray();
CollectionAssert.AreEqual(optionElements, optionList.ToArray());
IList<IWebElement> selectedOptionList = select.AllSelectedOptions;
IWebElement[] expectedSelection = { eggElement, sausageElement };
CollectionAssert.AreEqual(expectedSelection, selectedOptionList.ToArray());
select.SelectByValue("ham");
select.SelectByValue("onion gravy");
Assert.IsTrue(hamElement.Selected);
Assert.IsTrue(gravyElement.Selected);
select.DeselectByValue("eggs");
select.DeselectByValue("sausages");
Assert.IsFalse(eggElement.Selected);
Assert.IsFalse(sausageElement.Selected);
}
[TestMethod]
public void DisabledOption()
{
var selectElement = driver.FindElement(By.Name("single_disabled"));
var select = new SelectElement(selectElement);
Assert.ThrowsException<InvalidOperationException>(() => select.SelectByValue("disabled"));
}
}
}
option_list = select.options
Show full example
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Select List' do
let(:driver) { start_session }
before do
driver.get('https://www.selenium.dev/selenium/web/formPage.html')
end
it 'select options' do
select_element = driver.find_element(name: 'selectomatic')
select = Selenium::WebDriver::Support::Select.new(select_element)
two_element = driver.find_element(css: 'option[value=two]')
four_element = driver.find_element(css: 'option[value=four]')
count_element = driver.find_element(css: "option[value='still learning how to count, apparently']")
select.select_by(:text, 'Four')
expect(four_element).to be_selected
select.select_by(:value, 'two')
expect(two_element).to be_selected
select.select_by(:index, 3)
expect(count_element).to be_selected
end
it 'select multiple options' do
select_element = driver.find_element(name: 'multi')
select = Selenium::WebDriver::Support::Select.new(select_element)
ham_element = driver.find_element(css: 'option[value=ham]')
gravy_element = driver.find_element(css: "option[value='onion gravy']")
egg_element = driver.find_element(css: 'option[value=eggs]')
sausage_element = driver.find_element(css: "option[value='sausages']")
option_elements = select_element.find_elements(tag_name: 'option')
option_list = select.options
expect(option_elements).to eq option_list
selected_option_list = select.selected_options
expected_selection = [egg_element, sausage_element]
expect(selected_option_list).to eq expected_selection
select.select_by(:value, 'ham')
select.select_by(:value, 'onion gravy')
expect(ham_element).to be_selected
expect(gravy_element).to be_selected
select.deselect_by(:value, 'eggs')
select.deselect_by(:value, 'sausages')
expect(egg_element).not_to be_selected
expect(sausage_element).not_to be_selected
end
it 'disabled options' do
select_element = driver.find_element(name: 'single_disabled')
select = Selenium::WebDriver::Support::Select.new(select_element)
expect {
select.select_by(:value, 'disabled')
}.to raise_exception(Selenium::WebDriver::Error::UnsupportedOperationError)
end
end
Show full example
const {By, Browser, Builder} = require('selenium-webdriver')
const assert = require('assert/strict')
const {Select} = require('selenium-webdriver')
describe('Select Tests', async function () {
let driver
before(async function () {
driver = new Builder()
.forBrowser(Browser.FIREFOX)
.build()
await driver.get('https://www.selenium.dev/selenium/web/formPage.html')
})
after(async () => await driver.quit())
it('Select an option', async function () {
const selectElement = await driver.findElement(By.name('selectomatic'))
const select = new Select(selectElement)
const twoElement = await driver.findElement(By.css('option[value=two]'))
const fourElement = await driver.findElement(By.css('option[value=four]'))
const countElement = await driver.findElement(By.css("option[value='still learning how to count, apparently']"))
await select.selectByVisibleText('Four')
assert.equal(true, await fourElement.isSelected())
await select.selectByValue('two')
assert.equal(true, await twoElement.isSelected())
await select.selectByIndex(3)
assert.equal(true, await countElement.isSelected())
})
it('Select by multiple options', async function () {
const selectElement = await driver.findElement(By.name('multi'))
const select = await new Select(selectElement)
const hamElement = await driver.findElement(By.css('option[value=ham]'))
const gravyElement = await driver.findElement(By.css("option[value='onion gravy']"))
const eggElement = await driver.findElement(By.css('option[value=eggs]'))
const sausageElement = await driver.findElement(By.css("option[value='sausages']"))
const optionElements = await selectElement.findElements(By.css('option'))
const optionList = await select.getOptions()
assert.equal(optionList.length, optionElements.length)
for (const index in optionList) {
assert.equal(await optionList[index].getText(), await optionElements[index].getText())
}
const selectedOptionList = await select.getAllSelectedOptions()
const expectedSelection = [eggElement, sausageElement]
assert.equal(expectedSelection.length, selectedOptionList.length)
for (const index in selectedOptionList) {
assert.equal(await selectedOptionList[index].getText(), await expectedSelection[index].getText())
}
await select.selectByValue('ham')
await select.selectByValue('onion gravy')
assert.equal(true, await hamElement.isSelected())
assert.equal(true, await gravyElement.isSelected())
await select.deselectByValue('eggs')
await select.deselectByValue('sausages')
assert.equal(false, await eggElement.isSelected())
assert.equal(false, await sausageElement.isSelected())
})
it('Try selecting disabled option', async function () {
const selectElement = await driver.findElement(By.name('single_disabled'))
const select = await new Select(selectElement)
await assert.rejects(async () => {
await select.selectByValue("disabled")
}, {
name: 'UnsupportedOperationError',
message: 'You may not select a disabled option'
})
})
})
val optionList = select.getOptions()
Show full example
package dev.selenium.support
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.ui.Select
class SelectListTest : BaseTest() {
@BeforeEach
fun navigate() {
driver.get("https://www.selenium.dev/selenium/web/formPage.html")
}
@Test
fun selectOption() {
val selectElement = driver.findElement(By.name("selectomatic"))
val select = Select(selectElement)
val twoElement = driver.findElement(By.cssSelector("option[value=two]"))
val fourElement = driver.findElement(By.cssSelector("option[value=four]"))
val countElement = driver.findElement(By.cssSelector("option[value='still learning how to count, apparently']"))
select.selectByVisibleText("Four")
Assertions.assertTrue(fourElement.isSelected())
select.selectByValue("two")
Assertions.assertTrue(twoElement.isSelected())
select.selectByIndex(3)
Assertions.assertTrue(countElement.isSelected())
}
@Test
fun selectMultipleOption() {
val selectElement = driver.findElement(By.name("multi"))
val select = Select(selectElement)
val hamElement = driver.findElement(By.cssSelector("option[value=ham]"))
val gravyElement = driver.findElement(By.cssSelector("option[value='onion gravy']"))
val eggElement = driver.findElement(By.cssSelector("option[value=eggs]"))
val sausageElement = driver.findElement(By.cssSelector("option[value='sausages']"))
val optionElements = selectElement.findElements(By.tagName("option"))
val optionList = select.getOptions()
Assertions.assertEquals(optionElements, optionList)
val selectedOptionList = select.getAllSelectedOptions()
val expectedSelection = ArrayList<WebElement>()
expectedSelection.add(eggElement)
expectedSelection.add(sausageElement)
Assertions.assertEquals(expectedSelection, selectedOptionList)
select.selectByValue("ham")
select.selectByValue("onion gravy")
Assertions.assertTrue(hamElement.isSelected())
Assertions.assertTrue(gravyElement.isSelected())
select.deselectByValue("eggs")
select.deselectByValue("sausages")
Assertions.assertFalse(eggElement.isSelected())
Assertions.assertFalse(sausageElement.isSelected())
}
@Test
fun disabledOption() {
val selectElement = driver.findElement(By.name("single_disabled"))
val select = Select(selectElement)
Assertions.assertThrows(UnsupportedOperationException::class.java) {
select.selectByValue("disabled")
}
}
}
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.
List<WebElement> selectedOptionList = select.getAllSelectedOptions();
Show full example
package dev.selenium.support;
import dev.selenium.BaseChromeTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import java.util.ArrayList;
import java.util.List;
public class SelectListTest extends BaseChromeTest {
@BeforeEach
public void navigate() {
driver.get("https://www.selenium.dev/selenium/web/formPage.html");
}
@Test
public void selectOption() {
WebElement selectElement = driver.findElement(By.name("selectomatic"));
Select select = new Select(selectElement);
WebElement twoElement = driver.findElement(By.cssSelector("option[value=two]"));
WebElement fourElement = driver.findElement(By.cssSelector("option[value=four]"));
WebElement countElement = driver.findElement(By.cssSelector("option[value='still learning how to count, apparently']"));
select.selectByVisibleText("Four");
Assertions.assertTrue(fourElement.isSelected());
select.selectByValue("two");
Assertions.assertTrue(twoElement.isSelected());
select.selectByIndex(3);
Assertions.assertTrue(countElement.isSelected());
}
@Test
public void selectMultipleOption() {
WebElement selectElement = driver.findElement(By.name("multi"));
Select select = new Select(selectElement);
WebElement hamElement = driver.findElement(By.cssSelector("option[value=ham]"));
WebElement gravyElement = driver.findElement(By.cssSelector("option[value='onion gravy']"));
WebElement eggElement = driver.findElement(By.cssSelector("option[value=eggs]"));
WebElement sausageElement = driver.findElement(By.cssSelector("option[value='sausages']"));
List<WebElement> optionElements = selectElement.findElements(By.tagName("option"));
List<WebElement> optionList = select.getOptions();
Assertions.assertEquals(optionElements, optionList);
List<WebElement> selectedOptionList = select.getAllSelectedOptions();
List<WebElement> expectedSelection = new ArrayList<WebElement>() {{
add(eggElement);
add(sausageElement);
}};
Assertions.assertEquals(expectedSelection, selectedOptionList);
select.selectByValue("ham");
select.selectByValue("onion gravy");
Assertions.assertTrue(hamElement.isSelected());
Assertions.assertTrue(gravyElement.isSelected());
select.deselectByValue("eggs");
select.deselectByValue("sausages");
Assertions.assertFalse(eggElement.isSelected());
Assertions.assertFalse(sausageElement.isSelected());
}
@Test
public void disabledOption() {
WebElement selectElement = driver.findElement(By.name("single_disabled"));
Select select = new Select(selectElement);
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
select.selectByValue("disabled");
});
}
}
selected_option_list = select.all_selected_options
Show full example
import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
def test_select_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'selectomatic')
select = Select(select_element)
two_element = driver.find_element(By.CSS_SELECTOR, 'option[value=two]')
four_element = driver.find_element(By.CSS_SELECTOR, 'option[value=four]')
count_element = driver.find_element(By.CSS_SELECTOR, "option[value='still learning how to count, apparently']")
select.select_by_visible_text('Four')
assert four_element.is_selected()
select.select_by_value('two')
assert two_element.is_selected()
select.select_by_index(3)
assert count_element.is_selected()
def test_select_multiple_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'multi')
select = Select(select_element)
ham_element = driver.find_element(By.CSS_SELECTOR, 'option[value=ham]')
gravy_element = driver.find_element(By.CSS_SELECTOR, "option[value='onion gravy']")
egg_element = driver.find_element(By.CSS_SELECTOR, 'option[value=eggs]')
sausage_element = driver.find_element(By.CSS_SELECTOR, "option[value='sausages']")
option_elements = select_element.find_elements(By.TAG_NAME, 'option')
option_list = select.options
assert option_elements == option_list
selected_option_list = select.all_selected_options
expected_selection = [egg_element, sausage_element]
assert selected_option_list == expected_selection
select.select_by_value('ham')
select.select_by_value('onion gravy')
assert ham_element.is_selected()
assert gravy_element.is_selected()
select.deselect_by_value('eggs')
select.deselect_by_value('sausages')
assert not egg_element.is_selected()
assert not sausage_element.is_selected()
def test_disabled_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'single_disabled')
select = Select(select_element)
with pytest.raises(NotImplementedError):
select.select_by_value('disabled')
IList<IWebElement> selectedOptionList = select.AllSelectedOptions;
Show full example
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace SeleniumDocs.Support
{
[TestClass]
public class SelectListTest : BaseChromeTest
{
[TestInitialize]
public void Navigate()
{
driver.Url = "https://www.selenium.dev/selenium/web/formPage.html";
}
[TestMethod]
public void SelectOption()
{
var selectElement = driver.FindElement(By.Name("selectomatic"));
var select = new SelectElement(selectElement);
var twoElement = driver.FindElement(By.CssSelector("option[value=two]"));
var fourElement = driver.FindElement(By.CssSelector("option[value=four]"));
var countElement = driver.FindElement(By.CssSelector("option[value='still learning how to count, apparently']"));
select.SelectByText("Four");
Assert.IsTrue(fourElement.Selected);
select.SelectByValue("two");
Assert.IsTrue(twoElement.Selected);
select.SelectByIndex(3);
Assert.IsTrue(countElement.Selected);
}
[TestMethod]
public void SelectMultipleOption()
{
var selectElement = driver.FindElement(By.Name("multi"));
var select = new SelectElement(selectElement);
var hamElement = driver.FindElement(By.CssSelector("option[value=ham]"));
var gravyElement = driver.FindElement(By.CssSelector("option[value='onion gravy']"));
var eggElement = driver.FindElement(By.CssSelector("option[value=eggs]"));
var sausageElement = driver.FindElement(By.CssSelector("option[value='sausages']"));
IList<IWebElement> optionList = select.Options;
IWebElement[] optionElements = selectElement.FindElements(By.TagName("option")).ToArray();
CollectionAssert.AreEqual(optionElements, optionList.ToArray());
IList<IWebElement> selectedOptionList = select.AllSelectedOptions;
IWebElement[] expectedSelection = { eggElement, sausageElement };
CollectionAssert.AreEqual(expectedSelection, selectedOptionList.ToArray());
select.SelectByValue("ham");
select.SelectByValue("onion gravy");
Assert.IsTrue(hamElement.Selected);
Assert.IsTrue(gravyElement.Selected);
select.DeselectByValue("eggs");
select.DeselectByValue("sausages");
Assert.IsFalse(eggElement.Selected);
Assert.IsFalse(sausageElement.Selected);
}
[TestMethod]
public void DisabledOption()
{
var selectElement = driver.FindElement(By.Name("single_disabled"));
var select = new SelectElement(selectElement);
Assert.ThrowsException<InvalidOperationException>(() => select.SelectByValue("disabled"));
}
}
}
selected_option_list = select.selected_options
Show full example
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Select List' do
let(:driver) { start_session }
before do
driver.get('https://www.selenium.dev/selenium/web/formPage.html')
end
it 'select options' do
select_element = driver.find_element(name: 'selectomatic')
select = Selenium::WebDriver::Support::Select.new(select_element)
two_element = driver.find_element(css: 'option[value=two]')
four_element = driver.find_element(css: 'option[value=four]')
count_element = driver.find_element(css: "option[value='still learning how to count, apparently']")
select.select_by(:text, 'Four')
expect(four_element).to be_selected
select.select_by(:value, 'two')
expect(two_element).to be_selected
select.select_by(:index, 3)
expect(count_element).to be_selected
end
it 'select multiple options' do
select_element = driver.find_element(name: 'multi')
select = Selenium::WebDriver::Support::Select.new(select_element)
ham_element = driver.find_element(css: 'option[value=ham]')
gravy_element = driver.find_element(css: "option[value='onion gravy']")
egg_element = driver.find_element(css: 'option[value=eggs]')
sausage_element = driver.find_element(css: "option[value='sausages']")
option_elements = select_element.find_elements(tag_name: 'option')
option_list = select.options
expect(option_elements).to eq option_list
selected_option_list = select.selected_options
expected_selection = [egg_element, sausage_element]
expect(selected_option_list).to eq expected_selection
select.select_by(:value, 'ham')
select.select_by(:value, 'onion gravy')
expect(ham_element).to be_selected
expect(gravy_element).to be_selected
select.deselect_by(:value, 'eggs')
select.deselect_by(:value, 'sausages')
expect(egg_element).not_to be_selected
expect(sausage_element).not_to be_selected
end
it 'disabled options' do
select_element = driver.find_element(name: 'single_disabled')
select = Selenium::WebDriver::Support::Select.new(select_element)
expect {
select.select_by(:value, 'disabled')
}.to raise_exception(Selenium::WebDriver::Error::UnsupportedOperationError)
end
end
}
Show full example
const {By, Browser, Builder} = require('selenium-webdriver')
const assert = require('assert/strict')
const {Select} = require('selenium-webdriver')
describe('Select Tests', async function () {
let driver
before(async function () {
driver = new Builder()
.forBrowser(Browser.FIREFOX)
.build()
await driver.get('https://www.selenium.dev/selenium/web/formPage.html')
})
after(async () => await driver.quit())
it('Select an option', async function () {
const selectElement = await driver.findElement(By.name('selectomatic'))
const select = new Select(selectElement)
const twoElement = await driver.findElement(By.css('option[value=two]'))
const fourElement = await driver.findElement(By.css('option[value=four]'))
const countElement = await driver.findElement(By.css("option[value='still learning how to count, apparently']"))
await select.selectByVisibleText('Four')
assert.equal(true, await fourElement.isSelected())
await select.selectByValue('two')
assert.equal(true, await twoElement.isSelected())
await select.selectByIndex(3)
assert.equal(true, await countElement.isSelected())
})
it('Select by multiple options', async function () {
const selectElement = await driver.findElement(By.name('multi'))
const select = await new Select(selectElement)
const hamElement = await driver.findElement(By.css('option[value=ham]'))
const gravyElement = await driver.findElement(By.css("option[value='onion gravy']"))
const eggElement = await driver.findElement(By.css('option[value=eggs]'))
const sausageElement = await driver.findElement(By.css("option[value='sausages']"))
const optionElements = await selectElement.findElements(By.css('option'))
const optionList = await select.getOptions()
assert.equal(optionList.length, optionElements.length)
for (const index in optionList) {
assert.equal(await optionList[index].getText(), await optionElements[index].getText())
}
const selectedOptionList = await select.getAllSelectedOptions()
const expectedSelection = [eggElement, sausageElement]
assert.equal(expectedSelection.length, selectedOptionList.length)
for (const index in selectedOptionList) {
assert.equal(await selectedOptionList[index].getText(), await expectedSelection[index].getText())
}
await select.selectByValue('ham')
await select.selectByValue('onion gravy')
assert.equal(true, await hamElement.isSelected())
assert.equal(true, await gravyElement.isSelected())
await select.deselectByValue('eggs')
await select.deselectByValue('sausages')
assert.equal(false, await eggElement.isSelected())
assert.equal(false, await sausageElement.isSelected())
})
it('Try selecting disabled option', async function () {
const selectElement = await driver.findElement(By.name('single_disabled'))
const select = await new Select(selectElement)
await assert.rejects(async () => {
await select.selectByValue("disabled")
}, {
name: 'UnsupportedOperationError',
message: 'You may not select a disabled option'
})
})
})
val selectedOptionList = select.getAllSelectedOptions()
Show full example
package dev.selenium.support
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.ui.Select
class SelectListTest : BaseTest() {
@BeforeEach
fun navigate() {
driver.get("https://www.selenium.dev/selenium/web/formPage.html")
}
@Test
fun selectOption() {
val selectElement = driver.findElement(By.name("selectomatic"))
val select = Select(selectElement)
val twoElement = driver.findElement(By.cssSelector("option[value=two]"))
val fourElement = driver.findElement(By.cssSelector("option[value=four]"))
val countElement = driver.findElement(By.cssSelector("option[value='still learning how to count, apparently']"))
select.selectByVisibleText("Four")
Assertions.assertTrue(fourElement.isSelected())
select.selectByValue("two")
Assertions.assertTrue(twoElement.isSelected())
select.selectByIndex(3)
Assertions.assertTrue(countElement.isSelected())
}
@Test
fun selectMultipleOption() {
val selectElement = driver.findElement(By.name("multi"))
val select = Select(selectElement)
val hamElement = driver.findElement(By.cssSelector("option[value=ham]"))
val gravyElement = driver.findElement(By.cssSelector("option[value='onion gravy']"))
val eggElement = driver.findElement(By.cssSelector("option[value=eggs]"))
val sausageElement = driver.findElement(By.cssSelector("option[value='sausages']"))
val optionElements = selectElement.findElements(By.tagName("option"))
val optionList = select.getOptions()
Assertions.assertEquals(optionElements, optionList)
val selectedOptionList = select.getAllSelectedOptions()
val expectedSelection = ArrayList<WebElement>()
expectedSelection.add(eggElement)
expectedSelection.add(sausageElement)
Assertions.assertEquals(expectedSelection, selectedOptionList)
select.selectByValue("ham")
select.selectByValue("onion gravy")
Assertions.assertTrue(hamElement.isSelected())
Assertions.assertTrue(gravyElement.isSelected())
select.deselectByValue("eggs")
select.deselectByValue("sausages")
Assertions.assertFalse(eggElement.isSelected())
Assertions.assertFalse(sausageElement.isSelected())
}
@Test
fun disabledOption() {
val selectElement = driver.findElement(By.name("single_disabled"))
val select = Select(selectElement)
Assertions.assertThrows(UnsupportedOperationException::class.java) {
select.selectByValue("disabled")
}
}
}
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
select.selectByVisibleText("Four");
Show full example
package dev.selenium.support;
import dev.selenium.BaseChromeTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import java.util.ArrayList;
import java.util.List;
public class SelectListTest extends BaseChromeTest {
@BeforeEach
public void navigate() {
driver.get("https://www.selenium.dev/selenium/web/formPage.html");
}
@Test
public void selectOption() {
WebElement selectElement = driver.findElement(By.name("selectomatic"));
Select select = new Select(selectElement);
WebElement twoElement = driver.findElement(By.cssSelector("option[value=two]"));
WebElement fourElement = driver.findElement(By.cssSelector("option[value=four]"));
WebElement countElement = driver.findElement(By.cssSelector("option[value='still learning how to count, apparently']"));
select.selectByVisibleText("Four");
Assertions.assertTrue(fourElement.isSelected());
select.selectByValue("two");
Assertions.assertTrue(twoElement.isSelected());
select.selectByIndex(3);
Assertions.assertTrue(countElement.isSelected());
}
@Test
public void selectMultipleOption() {
WebElement selectElement = driver.findElement(By.name("multi"));
Select select = new Select(selectElement);
WebElement hamElement = driver.findElement(By.cssSelector("option[value=ham]"));
WebElement gravyElement = driver.findElement(By.cssSelector("option[value='onion gravy']"));
WebElement eggElement = driver.findElement(By.cssSelector("option[value=eggs]"));
WebElement sausageElement = driver.findElement(By.cssSelector("option[value='sausages']"));
List<WebElement> optionElements = selectElement.findElements(By.tagName("option"));
List<WebElement> optionList = select.getOptions();
Assertions.assertEquals(optionElements, optionList);
List<WebElement> selectedOptionList = select.getAllSelectedOptions();
List<WebElement> expectedSelection = new ArrayList<WebElement>() {{
add(eggElement);
add(sausageElement);
}};
Assertions.assertEquals(expectedSelection, selectedOptionList);
select.selectByValue("ham");
select.selectByValue("onion gravy");
Assertions.assertTrue(hamElement.isSelected());
Assertions.assertTrue(gravyElement.isSelected());
select.deselectByValue("eggs");
select.deselectByValue("sausages");
Assertions.assertFalse(eggElement.isSelected());
Assertions.assertFalse(sausageElement.isSelected());
}
@Test
public void disabledOption() {
WebElement selectElement = driver.findElement(By.name("single_disabled"));
Select select = new Select(selectElement);
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
select.selectByValue("disabled");
});
}
}
select.select_by_visible_text('Four')
Show full example
import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
def test_select_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'selectomatic')
select = Select(select_element)
two_element = driver.find_element(By.CSS_SELECTOR, 'option[value=two]')
four_element = driver.find_element(By.CSS_SELECTOR, 'option[value=four]')
count_element = driver.find_element(By.CSS_SELECTOR, "option[value='still learning how to count, apparently']")
select.select_by_visible_text('Four')
assert four_element.is_selected()
select.select_by_value('two')
assert two_element.is_selected()
select.select_by_index(3)
assert count_element.is_selected()
def test_select_multiple_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'multi')
select = Select(select_element)
ham_element = driver.find_element(By.CSS_SELECTOR, 'option[value=ham]')
gravy_element = driver.find_element(By.CSS_SELECTOR, "option[value='onion gravy']")
egg_element = driver.find_element(By.CSS_SELECTOR, 'option[value=eggs]')
sausage_element = driver.find_element(By.CSS_SELECTOR, "option[value='sausages']")
option_elements = select_element.find_elements(By.TAG_NAME, 'option')
option_list = select.options
assert option_elements == option_list
selected_option_list = select.all_selected_options
expected_selection = [egg_element, sausage_element]
assert selected_option_list == expected_selection
select.select_by_value('ham')
select.select_by_value('onion gravy')
assert ham_element.is_selected()
assert gravy_element.is_selected()
select.deselect_by_value('eggs')
select.deselect_by_value('sausages')
assert not egg_element.is_selected()
assert not sausage_element.is_selected()
def test_disabled_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'single_disabled')
select = Select(select_element)
with pytest.raises(NotImplementedError):
select.select_by_value('disabled')
select.SelectByText("Four");
Show full example
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace SeleniumDocs.Support
{
[TestClass]
public class SelectListTest : BaseChromeTest
{
[TestInitialize]
public void Navigate()
{
driver.Url = "https://www.selenium.dev/selenium/web/formPage.html";
}
[TestMethod]
public void SelectOption()
{
var selectElement = driver.FindElement(By.Name("selectomatic"));
var select = new SelectElement(selectElement);
var twoElement = driver.FindElement(By.CssSelector("option[value=two]"));
var fourElement = driver.FindElement(By.CssSelector("option[value=four]"));
var countElement = driver.FindElement(By.CssSelector("option[value='still learning how to count, apparently']"));
select.SelectByText("Four");
Assert.IsTrue(fourElement.Selected);
select.SelectByValue("two");
Assert.IsTrue(twoElement.Selected);
select.SelectByIndex(3);
Assert.IsTrue(countElement.Selected);
}
[TestMethod]
public void SelectMultipleOption()
{
var selectElement = driver.FindElement(By.Name("multi"));
var select = new SelectElement(selectElement);
var hamElement = driver.FindElement(By.CssSelector("option[value=ham]"));
var gravyElement = driver.FindElement(By.CssSelector("option[value='onion gravy']"));
var eggElement = driver.FindElement(By.CssSelector("option[value=eggs]"));
var sausageElement = driver.FindElement(By.CssSelector("option[value='sausages']"));
IList<IWebElement> optionList = select.Options;
IWebElement[] optionElements = selectElement.FindElements(By.TagName("option")).ToArray();
CollectionAssert.AreEqual(optionElements, optionList.ToArray());
IList<IWebElement> selectedOptionList = select.AllSelectedOptions;
IWebElement[] expectedSelection = { eggElement, sausageElement };
CollectionAssert.AreEqual(expectedSelection, selectedOptionList.ToArray());
select.SelectByValue("ham");
select.SelectByValue("onion gravy");
Assert.IsTrue(hamElement.Selected);
Assert.IsTrue(gravyElement.Selected);
select.DeselectByValue("eggs");
select.DeselectByValue("sausages");
Assert.IsFalse(eggElement.Selected);
Assert.IsFalse(sausageElement.Selected);
}
[TestMethod]
public void DisabledOption()
{
var selectElement = driver.FindElement(By.Name("single_disabled"));
var select = new SelectElement(selectElement);
Assert.ThrowsException<InvalidOperationException>(() => select.SelectByValue("disabled"));
}
}
}
select.select_by(:text, 'Four')
Show full example
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Select List' do
let(:driver) { start_session }
before do
driver.get('https://www.selenium.dev/selenium/web/formPage.html')
end
it 'select options' do
select_element = driver.find_element(name: 'selectomatic')
select = Selenium::WebDriver::Support::Select.new(select_element)
two_element = driver.find_element(css: 'option[value=two]')
four_element = driver.find_element(css: 'option[value=four]')
count_element = driver.find_element(css: "option[value='still learning how to count, apparently']")
select.select_by(:text, 'Four')
expect(four_element).to be_selected
select.select_by(:value, 'two')
expect(two_element).to be_selected
select.select_by(:index, 3)
expect(count_element).to be_selected
end
it 'select multiple options' do
select_element = driver.find_element(name: 'multi')
select = Selenium::WebDriver::Support::Select.new(select_element)
ham_element = driver.find_element(css: 'option[value=ham]')
gravy_element = driver.find_element(css: "option[value='onion gravy']")
egg_element = driver.find_element(css: 'option[value=eggs]')
sausage_element = driver.find_element(css: "option[value='sausages']")
option_elements = select_element.find_elements(tag_name: 'option')
option_list = select.options
expect(option_elements).to eq option_list
selected_option_list = select.selected_options
expected_selection = [egg_element, sausage_element]
expect(selected_option_list).to eq expected_selection
select.select_by(:value, 'ham')
select.select_by(:value, 'onion gravy')
expect(ham_element).to be_selected
expect(gravy_element).to be_selected
select.deselect_by(:value, 'eggs')
select.deselect_by(:value, 'sausages')
expect(egg_element).not_to be_selected
expect(sausage_element).not_to be_selected
end
it 'disabled options' do
select_element = driver.find_element(name: 'single_disabled')
select = Selenium::WebDriver::Support::Select.new(select_element)
expect {
select.select_by(:value, 'disabled')
}.to raise_exception(Selenium::WebDriver::Error::UnsupportedOperationError)
end
end
const countElement = await driver.findElement(By.css("option[value='still learning how to count, apparently']"))
Show full example
const {By, Browser, Builder} = require('selenium-webdriver')
const assert = require('assert/strict')
const {Select} = require('selenium-webdriver')
describe('Select Tests', async function () {
let driver
before(async function () {
driver = new Builder()
.forBrowser(Browser.FIREFOX)
.build()
await driver.get('https://www.selenium.dev/selenium/web/formPage.html')
})
after(async () => await driver.quit())
it('Select an option', async function () {
const selectElement = await driver.findElement(By.name('selectomatic'))
const select = new Select(selectElement)
const twoElement = await driver.findElement(By.css('option[value=two]'))
const fourElement = await driver.findElement(By.css('option[value=four]'))
const countElement = await driver.findElement(By.css("option[value='still learning how to count, apparently']"))
await select.selectByVisibleText('Four')
assert.equal(true, await fourElement.isSelected())
await select.selectByValue('two')
assert.equal(true, await twoElement.isSelected())
await select.selectByIndex(3)
assert.equal(true, await countElement.isSelected())
})
it('Select by multiple options', async function () {
const selectElement = await driver.findElement(By.name('multi'))
const select = await new Select(selectElement)
const hamElement = await driver.findElement(By.css('option[value=ham]'))
const gravyElement = await driver.findElement(By.css("option[value='onion gravy']"))
const eggElement = await driver.findElement(By.css('option[value=eggs]'))
const sausageElement = await driver.findElement(By.css("option[value='sausages']"))
const optionElements = await selectElement.findElements(By.css('option'))
const optionList = await select.getOptions()
assert.equal(optionList.length, optionElements.length)
for (const index in optionList) {
assert.equal(await optionList[index].getText(), await optionElements[index].getText())
}
const selectedOptionList = await select.getAllSelectedOptions()
const expectedSelection = [eggElement, sausageElement]
assert.equal(expectedSelection.length, selectedOptionList.length)
for (const index in selectedOptionList) {
assert.equal(await selectedOptionList[index].getText(), await expectedSelection[index].getText())
}
await select.selectByValue('ham')
await select.selectByValue('onion gravy')
assert.equal(true, await hamElement.isSelected())
assert.equal(true, await gravyElement.isSelected())
await select.deselectByValue('eggs')
await select.deselectByValue('sausages')
assert.equal(false, await eggElement.isSelected())
assert.equal(false, await sausageElement.isSelected())
})
it('Try selecting disabled option', async function () {
const selectElement = await driver.findElement(By.name('single_disabled'))
const select = await new Select(selectElement)
await assert.rejects(async () => {
await select.selectByValue("disabled")
}, {
name: 'UnsupportedOperationError',
message: 'You may not select a disabled option'
})
})
})
select.selectByVisibleText("Four")
Show full example
package dev.selenium.support
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.ui.Select
class SelectListTest : BaseTest() {
@BeforeEach
fun navigate() {
driver.get("https://www.selenium.dev/selenium/web/formPage.html")
}
@Test
fun selectOption() {
val selectElement = driver.findElement(By.name("selectomatic"))
val select = Select(selectElement)
val twoElement = driver.findElement(By.cssSelector("option[value=two]"))
val fourElement = driver.findElement(By.cssSelector("option[value=four]"))
val countElement = driver.findElement(By.cssSelector("option[value='still learning how to count, apparently']"))
select.selectByVisibleText("Four")
Assertions.assertTrue(fourElement.isSelected())
select.selectByValue("two")
Assertions.assertTrue(twoElement.isSelected())
select.selectByIndex(3)
Assertions.assertTrue(countElement.isSelected())
}
@Test
fun selectMultipleOption() {
val selectElement = driver.findElement(By.name("multi"))
val select = Select(selectElement)
val hamElement = driver.findElement(By.cssSelector("option[value=ham]"))
val gravyElement = driver.findElement(By.cssSelector("option[value='onion gravy']"))
val eggElement = driver.findElement(By.cssSelector("option[value=eggs]"))
val sausageElement = driver.findElement(By.cssSelector("option[value='sausages']"))
val optionElements = selectElement.findElements(By.tagName("option"))
val optionList = select.getOptions()
Assertions.assertEquals(optionElements, optionList)
val selectedOptionList = select.getAllSelectedOptions()
val expectedSelection = ArrayList<WebElement>()
expectedSelection.add(eggElement)
expectedSelection.add(sausageElement)
Assertions.assertEquals(expectedSelection, selectedOptionList)
select.selectByValue("ham")
select.selectByValue("onion gravy")
Assertions.assertTrue(hamElement.isSelected())
Assertions.assertTrue(gravyElement.isSelected())
select.deselectByValue("eggs")
select.deselectByValue("sausages")
Assertions.assertFalse(eggElement.isSelected())
Assertions.assertFalse(sausageElement.isSelected())
}
@Test
fun disabledOption() {
val selectElement = driver.findElement(By.name("single_disabled"))
val select = Select(selectElement)
Assertions.assertThrows(UnsupportedOperationException::class.java) {
select.selectByValue("disabled")
}
}
}
Value
Select the option based on its value attribute
select.selectByValue("two");
Show full example
package dev.selenium.support;
import dev.selenium.BaseChromeTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import java.util.ArrayList;
import java.util.List;
public class SelectListTest extends BaseChromeTest {
@BeforeEach
public void navigate() {
driver.get("https://www.selenium.dev/selenium/web/formPage.html");
}
@Test
public void selectOption() {
WebElement selectElement = driver.findElement(By.name("selectomatic"));
Select select = new Select(selectElement);
WebElement twoElement = driver.findElement(By.cssSelector("option[value=two]"));
WebElement fourElement = driver.findElement(By.cssSelector("option[value=four]"));
WebElement countElement = driver.findElement(By.cssSelector("option[value='still learning how to count, apparently']"));
select.selectByVisibleText("Four");
Assertions.assertTrue(fourElement.isSelected());
select.selectByValue("two");
Assertions.assertTrue(twoElement.isSelected());
select.selectByIndex(3);
Assertions.assertTrue(countElement.isSelected());
}
@Test
public void selectMultipleOption() {
WebElement selectElement = driver.findElement(By.name("multi"));
Select select = new Select(selectElement);
WebElement hamElement = driver.findElement(By.cssSelector("option[value=ham]"));
WebElement gravyElement = driver.findElement(By.cssSelector("option[value='onion gravy']"));
WebElement eggElement = driver.findElement(By.cssSelector("option[value=eggs]"));
WebElement sausageElement = driver.findElement(By.cssSelector("option[value='sausages']"));
List<WebElement> optionElements = selectElement.findElements(By.tagName("option"));
List<WebElement> optionList = select.getOptions();
Assertions.assertEquals(optionElements, optionList);
List<WebElement> selectedOptionList = select.getAllSelectedOptions();
List<WebElement> expectedSelection = new ArrayList<WebElement>() {{
add(eggElement);
add(sausageElement);
}};
Assertions.assertEquals(expectedSelection, selectedOptionList);
select.selectByValue("ham");
select.selectByValue("onion gravy");
Assertions.assertTrue(hamElement.isSelected());
Assertions.assertTrue(gravyElement.isSelected());
select.deselectByValue("eggs");
select.deselectByValue("sausages");
Assertions.assertFalse(eggElement.isSelected());
Assertions.assertFalse(sausageElement.isSelected());
}
@Test
public void disabledOption() {
WebElement selectElement = driver.findElement(By.name("single_disabled"));
Select select = new Select(selectElement);
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
select.selectByValue("disabled");
});
}
}
select.select_by_value('two')
Show full example
import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
def test_select_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'selectomatic')
select = Select(select_element)
two_element = driver.find_element(By.CSS_SELECTOR, 'option[value=two]')
four_element = driver.find_element(By.CSS_SELECTOR, 'option[value=four]')
count_element = driver.find_element(By.CSS_SELECTOR, "option[value='still learning how to count, apparently']")
select.select_by_visible_text('Four')
assert four_element.is_selected()
select.select_by_value('two')
assert two_element.is_selected()
select.select_by_index(3)
assert count_element.is_selected()
def test_select_multiple_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'multi')
select = Select(select_element)
ham_element = driver.find_element(By.CSS_SELECTOR, 'option[value=ham]')
gravy_element = driver.find_element(By.CSS_SELECTOR, "option[value='onion gravy']")
egg_element = driver.find_element(By.CSS_SELECTOR, 'option[value=eggs]')
sausage_element = driver.find_element(By.CSS_SELECTOR, "option[value='sausages']")
option_elements = select_element.find_elements(By.TAG_NAME, 'option')
option_list = select.options
assert option_elements == option_list
selected_option_list = select.all_selected_options
expected_selection = [egg_element, sausage_element]
assert selected_option_list == expected_selection
select.select_by_value('ham')
select.select_by_value('onion gravy')
assert ham_element.is_selected()
assert gravy_element.is_selected()
select.deselect_by_value('eggs')
select.deselect_by_value('sausages')
assert not egg_element.is_selected()
assert not sausage_element.is_selected()
def test_disabled_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'single_disabled')
select = Select(select_element)
with pytest.raises(NotImplementedError):
select.select_by_value('disabled')
select.SelectByValue("two");
Show full example
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace SeleniumDocs.Support
{
[TestClass]
public class SelectListTest : BaseChromeTest
{
[TestInitialize]
public void Navigate()
{
driver.Url = "https://www.selenium.dev/selenium/web/formPage.html";
}
[TestMethod]
public void SelectOption()
{
var selectElement = driver.FindElement(By.Name("selectomatic"));
var select = new SelectElement(selectElement);
var twoElement = driver.FindElement(By.CssSelector("option[value=two]"));
var fourElement = driver.FindElement(By.CssSelector("option[value=four]"));
var countElement = driver.FindElement(By.CssSelector("option[value='still learning how to count, apparently']"));
select.SelectByText("Four");
Assert.IsTrue(fourElement.Selected);
select.SelectByValue("two");
Assert.IsTrue(twoElement.Selected);
select.SelectByIndex(3);
Assert.IsTrue(countElement.Selected);
}
[TestMethod]
public void SelectMultipleOption()
{
var selectElement = driver.FindElement(By.Name("multi"));
var select = new SelectElement(selectElement);
var hamElement = driver.FindElement(By.CssSelector("option[value=ham]"));
var gravyElement = driver.FindElement(By.CssSelector("option[value='onion gravy']"));
var eggElement = driver.FindElement(By.CssSelector("option[value=eggs]"));
var sausageElement = driver.FindElement(By.CssSelector("option[value='sausages']"));
IList<IWebElement> optionList = select.Options;
IWebElement[] optionElements = selectElement.FindElements(By.TagName("option")).ToArray();
CollectionAssert.AreEqual(optionElements, optionList.ToArray());
IList<IWebElement> selectedOptionList = select.AllSelectedOptions;
IWebElement[] expectedSelection = { eggElement, sausageElement };
CollectionAssert.AreEqual(expectedSelection, selectedOptionList.ToArray());
select.SelectByValue("ham");
select.SelectByValue("onion gravy");
Assert.IsTrue(hamElement.Selected);
Assert.IsTrue(gravyElement.Selected);
select.DeselectByValue("eggs");
select.DeselectByValue("sausages");
Assert.IsFalse(eggElement.Selected);
Assert.IsFalse(sausageElement.Selected);
}
[TestMethod]
public void DisabledOption()
{
var selectElement = driver.FindElement(By.Name("single_disabled"));
var select = new SelectElement(selectElement);
Assert.ThrowsException<InvalidOperationException>(() => select.SelectByValue("disabled"));
}
}
}
select.select_by(:value, 'two')
Show full example
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Select List' do
let(:driver) { start_session }
before do
driver.get('https://www.selenium.dev/selenium/web/formPage.html')
end
it 'select options' do
select_element = driver.find_element(name: 'selectomatic')
select = Selenium::WebDriver::Support::Select.new(select_element)
two_element = driver.find_element(css: 'option[value=two]')
four_element = driver.find_element(css: 'option[value=four]')
count_element = driver.find_element(css: "option[value='still learning how to count, apparently']")
select.select_by(:text, 'Four')
expect(four_element).to be_selected
select.select_by(:value, 'two')
expect(two_element).to be_selected
select.select_by(:index, 3)
expect(count_element).to be_selected
end
it 'select multiple options' do
select_element = driver.find_element(name: 'multi')
select = Selenium::WebDriver::Support::Select.new(select_element)
ham_element = driver.find_element(css: 'option[value=ham]')
gravy_element = driver.find_element(css: "option[value='onion gravy']")
egg_element = driver.find_element(css: 'option[value=eggs]')
sausage_element = driver.find_element(css: "option[value='sausages']")
option_elements = select_element.find_elements(tag_name: 'option')
option_list = select.options
expect(option_elements).to eq option_list
selected_option_list = select.selected_options
expected_selection = [egg_element, sausage_element]
expect(selected_option_list).to eq expected_selection
select.select_by(:value, 'ham')
select.select_by(:value, 'onion gravy')
expect(ham_element).to be_selected
expect(gravy_element).to be_selected
select.deselect_by(:value, 'eggs')
select.deselect_by(:value, 'sausages')
expect(egg_element).not_to be_selected
expect(sausage_element).not_to be_selected
end
it 'disabled options' do
select_element = driver.find_element(name: 'single_disabled')
select = Selenium::WebDriver::Support::Select.new(select_element)
expect {
select.select_by(:value, 'disabled')
}.to raise_exception(Selenium::WebDriver::Error::UnsupportedOperationError)
end
end
assert.equal(true, await fourElement.isSelected())
Show full example
const {By, Browser, Builder} = require('selenium-webdriver')
const assert = require('assert/strict')
const {Select} = require('selenium-webdriver')
describe('Select Tests', async function () {
let driver
before(async function () {
driver = new Builder()
.forBrowser(Browser.FIREFOX)
.build()
await driver.get('https://www.selenium.dev/selenium/web/formPage.html')
})
after(async () => await driver.quit())
it('Select an option', async function () {
const selectElement = await driver.findElement(By.name('selectomatic'))
const select = new Select(selectElement)
const twoElement = await driver.findElement(By.css('option[value=two]'))
const fourElement = await driver.findElement(By.css('option[value=four]'))
const countElement = await driver.findElement(By.css("option[value='still learning how to count, apparently']"))
await select.selectByVisibleText('Four')
assert.equal(true, await fourElement.isSelected())
await select.selectByValue('two')
assert.equal(true, await twoElement.isSelected())
await select.selectByIndex(3)
assert.equal(true, await countElement.isSelected())
})
it('Select by multiple options', async function () {
const selectElement = await driver.findElement(By.name('multi'))
const select = await new Select(selectElement)
const hamElement = await driver.findElement(By.css('option[value=ham]'))
const gravyElement = await driver.findElement(By.css("option[value='onion gravy']"))
const eggElement = await driver.findElement(By.css('option[value=eggs]'))
const sausageElement = await driver.findElement(By.css("option[value='sausages']"))
const optionElements = await selectElement.findElements(By.css('option'))
const optionList = await select.getOptions()
assert.equal(optionList.length, optionElements.length)
for (const index in optionList) {
assert.equal(await optionList[index].getText(), await optionElements[index].getText())
}
const selectedOptionList = await select.getAllSelectedOptions()
const expectedSelection = [eggElement, sausageElement]
assert.equal(expectedSelection.length, selectedOptionList.length)
for (const index in selectedOptionList) {
assert.equal(await selectedOptionList[index].getText(), await expectedSelection[index].getText())
}
await select.selectByValue('ham')
await select.selectByValue('onion gravy')
assert.equal(true, await hamElement.isSelected())
assert.equal(true, await gravyElement.isSelected())
await select.deselectByValue('eggs')
await select.deselectByValue('sausages')
assert.equal(false, await eggElement.isSelected())
assert.equal(false, await sausageElement.isSelected())
})
it('Try selecting disabled option', async function () {
const selectElement = await driver.findElement(By.name('single_disabled'))
const select = await new Select(selectElement)
await assert.rejects(async () => {
await select.selectByValue("disabled")
}, {
name: 'UnsupportedOperationError',
message: 'You may not select a disabled option'
})
})
})
select.selectByValue("two")
Show full example
package dev.selenium.support
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.ui.Select
class SelectListTest : BaseTest() {
@BeforeEach
fun navigate() {
driver.get("https://www.selenium.dev/selenium/web/formPage.html")
}
@Test
fun selectOption() {
val selectElement = driver.findElement(By.name("selectomatic"))
val select = Select(selectElement)
val twoElement = driver.findElement(By.cssSelector("option[value=two]"))
val fourElement = driver.findElement(By.cssSelector("option[value=four]"))
val countElement = driver.findElement(By.cssSelector("option[value='still learning how to count, apparently']"))
select.selectByVisibleText("Four")
Assertions.assertTrue(fourElement.isSelected())
select.selectByValue("two")
Assertions.assertTrue(twoElement.isSelected())
select.selectByIndex(3)
Assertions.assertTrue(countElement.isSelected())
}
@Test
fun selectMultipleOption() {
val selectElement = driver.findElement(By.name("multi"))
val select = Select(selectElement)
val hamElement = driver.findElement(By.cssSelector("option[value=ham]"))
val gravyElement = driver.findElement(By.cssSelector("option[value='onion gravy']"))
val eggElement = driver.findElement(By.cssSelector("option[value=eggs]"))
val sausageElement = driver.findElement(By.cssSelector("option[value='sausages']"))
val optionElements = selectElement.findElements(By.tagName("option"))
val optionList = select.getOptions()
Assertions.assertEquals(optionElements, optionList)
val selectedOptionList = select.getAllSelectedOptions()
val expectedSelection = ArrayList<WebElement>()
expectedSelection.add(eggElement)
expectedSelection.add(sausageElement)
Assertions.assertEquals(expectedSelection, selectedOptionList)
select.selectByValue("ham")
select.selectByValue("onion gravy")
Assertions.assertTrue(hamElement.isSelected())
Assertions.assertTrue(gravyElement.isSelected())
select.deselectByValue("eggs")
select.deselectByValue("sausages")
Assertions.assertFalse(eggElement.isSelected())
Assertions.assertFalse(sausageElement.isSelected())
}
@Test
fun disabledOption() {
val selectElement = driver.findElement(By.name("single_disabled"))
val select = Select(selectElement)
Assertions.assertThrows(UnsupportedOperationException::class.java) {
select.selectByValue("disabled")
}
}
}
Index
Select the option based on its position in the list
select.selectByIndex(3);
Show full example
package dev.selenium.support;
import dev.selenium.BaseChromeTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import java.util.ArrayList;
import java.util.List;
public class SelectListTest extends BaseChromeTest {
@BeforeEach
public void navigate() {
driver.get("https://www.selenium.dev/selenium/web/formPage.html");
}
@Test
public void selectOption() {
WebElement selectElement = driver.findElement(By.name("selectomatic"));
Select select = new Select(selectElement);
WebElement twoElement = driver.findElement(By.cssSelector("option[value=two]"));
WebElement fourElement = driver.findElement(By.cssSelector("option[value=four]"));
WebElement countElement = driver.findElement(By.cssSelector("option[value='still learning how to count, apparently']"));
select.selectByVisibleText("Four");
Assertions.assertTrue(fourElement.isSelected());
select.selectByValue("two");
Assertions.assertTrue(twoElement.isSelected());
select.selectByIndex(3);
Assertions.assertTrue(countElement.isSelected());
}
@Test
public void selectMultipleOption() {
WebElement selectElement = driver.findElement(By.name("multi"));
Select select = new Select(selectElement);
WebElement hamElement = driver.findElement(By.cssSelector("option[value=ham]"));
WebElement gravyElement = driver.findElement(By.cssSelector("option[value='onion gravy']"));
WebElement eggElement = driver.findElement(By.cssSelector("option[value=eggs]"));
WebElement sausageElement = driver.findElement(By.cssSelector("option[value='sausages']"));
List<WebElement> optionElements = selectElement.findElements(By.tagName("option"));
List<WebElement> optionList = select.getOptions();
Assertions.assertEquals(optionElements, optionList);
List<WebElement> selectedOptionList = select.getAllSelectedOptions();
List<WebElement> expectedSelection = new ArrayList<WebElement>() {{
add(eggElement);
add(sausageElement);
}};
Assertions.assertEquals(expectedSelection, selectedOptionList);
select.selectByValue("ham");
select.selectByValue("onion gravy");
Assertions.assertTrue(hamElement.isSelected());
Assertions.assertTrue(gravyElement.isSelected());
select.deselectByValue("eggs");
select.deselectByValue("sausages");
Assertions.assertFalse(eggElement.isSelected());
Assertions.assertFalse(sausageElement.isSelected());
}
@Test
public void disabledOption() {
WebElement selectElement = driver.findElement(By.name("single_disabled"));
Select select = new Select(selectElement);
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
select.selectByValue("disabled");
});
}
}
select.select_by_index(3)
Show full example
import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
def test_select_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'selectomatic')
select = Select(select_element)
two_element = driver.find_element(By.CSS_SELECTOR, 'option[value=two]')
four_element = driver.find_element(By.CSS_SELECTOR, 'option[value=four]')
count_element = driver.find_element(By.CSS_SELECTOR, "option[value='still learning how to count, apparently']")
select.select_by_visible_text('Four')
assert four_element.is_selected()
select.select_by_value('two')
assert two_element.is_selected()
select.select_by_index(3)
assert count_element.is_selected()
def test_select_multiple_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'multi')
select = Select(select_element)
ham_element = driver.find_element(By.CSS_SELECTOR, 'option[value=ham]')
gravy_element = driver.find_element(By.CSS_SELECTOR, "option[value='onion gravy']")
egg_element = driver.find_element(By.CSS_SELECTOR, 'option[value=eggs]')
sausage_element = driver.find_element(By.CSS_SELECTOR, "option[value='sausages']")
option_elements = select_element.find_elements(By.TAG_NAME, 'option')
option_list = select.options
assert option_elements == option_list
selected_option_list = select.all_selected_options
expected_selection = [egg_element, sausage_element]
assert selected_option_list == expected_selection
select.select_by_value('ham')
select.select_by_value('onion gravy')
assert ham_element.is_selected()
assert gravy_element.is_selected()
select.deselect_by_value('eggs')
select.deselect_by_value('sausages')
assert not egg_element.is_selected()
assert not sausage_element.is_selected()
def test_disabled_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'single_disabled')
select = Select(select_element)
with pytest.raises(NotImplementedError):
select.select_by_value('disabled')
select.SelectByIndex(3);
Show full example
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace SeleniumDocs.Support
{
[TestClass]
public class SelectListTest : BaseChromeTest
{
[TestInitialize]
public void Navigate()
{
driver.Url = "https://www.selenium.dev/selenium/web/formPage.html";
}
[TestMethod]
public void SelectOption()
{
var selectElement = driver.FindElement(By.Name("selectomatic"));
var select = new SelectElement(selectElement);
var twoElement = driver.FindElement(By.CssSelector("option[value=two]"));
var fourElement = driver.FindElement(By.CssSelector("option[value=four]"));
var countElement = driver.FindElement(By.CssSelector("option[value='still learning how to count, apparently']"));
select.SelectByText("Four");
Assert.IsTrue(fourElement.Selected);
select.SelectByValue("two");
Assert.IsTrue(twoElement.Selected);
select.SelectByIndex(3);
Assert.IsTrue(countElement.Selected);
}
[TestMethod]
public void SelectMultipleOption()
{
var selectElement = driver.FindElement(By.Name("multi"));
var select = new SelectElement(selectElement);
var hamElement = driver.FindElement(By.CssSelector("option[value=ham]"));
var gravyElement = driver.FindElement(By.CssSelector("option[value='onion gravy']"));
var eggElement = driver.FindElement(By.CssSelector("option[value=eggs]"));
var sausageElement = driver.FindElement(By.CssSelector("option[value='sausages']"));
IList<IWebElement> optionList = select.Options;
IWebElement[] optionElements = selectElement.FindElements(By.TagName("option")).ToArray();
CollectionAssert.AreEqual(optionElements, optionList.ToArray());
IList<IWebElement> selectedOptionList = select.AllSelectedOptions;
IWebElement[] expectedSelection = { eggElement, sausageElement };
CollectionAssert.AreEqual(expectedSelection, selectedOptionList.ToArray());
select.SelectByValue("ham");
select.SelectByValue("onion gravy");
Assert.IsTrue(hamElement.Selected);
Assert.IsTrue(gravyElement.Selected);
select.DeselectByValue("eggs");
select.DeselectByValue("sausages");
Assert.IsFalse(eggElement.Selected);
Assert.IsFalse(sausageElement.Selected);
}
[TestMethod]
public void DisabledOption()
{
var selectElement = driver.FindElement(By.Name("single_disabled"));
var select = new SelectElement(selectElement);
Assert.ThrowsException<InvalidOperationException>(() => select.SelectByValue("disabled"));
}
}
}
select.select_by(:index, 3)
Show full example
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Select List' do
let(:driver) { start_session }
before do
driver.get('https://www.selenium.dev/selenium/web/formPage.html')
end
it 'select options' do
select_element = driver.find_element(name: 'selectomatic')
select = Selenium::WebDriver::Support::Select.new(select_element)
two_element = driver.find_element(css: 'option[value=two]')
four_element = driver.find_element(css: 'option[value=four]')
count_element = driver.find_element(css: "option[value='still learning how to count, apparently']")
select.select_by(:text, 'Four')
expect(four_element).to be_selected
select.select_by(:value, 'two')
expect(two_element).to be_selected
select.select_by(:index, 3)
expect(count_element).to be_selected
end
it 'select multiple options' do
select_element = driver.find_element(name: 'multi')
select = Selenium::WebDriver::Support::Select.new(select_element)
ham_element = driver.find_element(css: 'option[value=ham]')
gravy_element = driver.find_element(css: "option[value='onion gravy']")
egg_element = driver.find_element(css: 'option[value=eggs]')
sausage_element = driver.find_element(css: "option[value='sausages']")
option_elements = select_element.find_elements(tag_name: 'option')
option_list = select.options
expect(option_elements).to eq option_list
selected_option_list = select.selected_options
expected_selection = [egg_element, sausage_element]
expect(selected_option_list).to eq expected_selection
select.select_by(:value, 'ham')
select.select_by(:value, 'onion gravy')
expect(ham_element).to be_selected
expect(gravy_element).to be_selected
select.deselect_by(:value, 'eggs')
select.deselect_by(:value, 'sausages')
expect(egg_element).not_to be_selected
expect(sausage_element).not_to be_selected
end
it 'disabled options' do
select_element = driver.find_element(name: 'single_disabled')
select = Selenium::WebDriver::Support::Select.new(select_element)
expect {
select.select_by(:value, 'disabled')
}.to raise_exception(Selenium::WebDriver::Error::UnsupportedOperationError)
end
end
assert.equal(true, await twoElement.isSelected())
Show full example
const {By, Browser, Builder} = require('selenium-webdriver')
const assert = require('assert/strict')
const {Select} = require('selenium-webdriver')
describe('Select Tests', async function () {
let driver
before(async function () {
driver = new Builder()
.forBrowser(Browser.FIREFOX)
.build()
await driver.get('https://www.selenium.dev/selenium/web/formPage.html')
})
after(async () => await driver.quit())
it('Select an option', async function () {
const selectElement = await driver.findElement(By.name('selectomatic'))
const select = new Select(selectElement)
const twoElement = await driver.findElement(By.css('option[value=two]'))
const fourElement = await driver.findElement(By.css('option[value=four]'))
const countElement = await driver.findElement(By.css("option[value='still learning how to count, apparently']"))
await select.selectByVisibleText('Four')
assert.equal(true, await fourElement.isSelected())
await select.selectByValue('two')
assert.equal(true, await twoElement.isSelected())
await select.selectByIndex(3)
assert.equal(true, await countElement.isSelected())
})
it('Select by multiple options', async function () {
const selectElement = await driver.findElement(By.name('multi'))
const select = await new Select(selectElement)
const hamElement = await driver.findElement(By.css('option[value=ham]'))
const gravyElement = await driver.findElement(By.css("option[value='onion gravy']"))
const eggElement = await driver.findElement(By.css('option[value=eggs]'))
const sausageElement = await driver.findElement(By.css("option[value='sausages']"))
const optionElements = await selectElement.findElements(By.css('option'))
const optionList = await select.getOptions()
assert.equal(optionList.length, optionElements.length)
for (const index in optionList) {
assert.equal(await optionList[index].getText(), await optionElements[index].getText())
}
const selectedOptionList = await select.getAllSelectedOptions()
const expectedSelection = [eggElement, sausageElement]
assert.equal(expectedSelection.length, selectedOptionList.length)
for (const index in selectedOptionList) {
assert.equal(await selectedOptionList[index].getText(), await expectedSelection[index].getText())
}
await select.selectByValue('ham')
await select.selectByValue('onion gravy')
assert.equal(true, await hamElement.isSelected())
assert.equal(true, await gravyElement.isSelected())
await select.deselectByValue('eggs')
await select.deselectByValue('sausages')
assert.equal(false, await eggElement.isSelected())
assert.equal(false, await sausageElement.isSelected())
})
it('Try selecting disabled option', async function () {
const selectElement = await driver.findElement(By.name('single_disabled'))
const select = await new Select(selectElement)
await assert.rejects(async () => {
await select.selectByValue("disabled")
}, {
name: 'UnsupportedOperationError',
message: 'You may not select a disabled option'
})
})
})
select.selectByIndex(3)
Show full example
package dev.selenium.support
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.ui.Select
class SelectListTest : BaseTest() {
@BeforeEach
fun navigate() {
driver.get("https://www.selenium.dev/selenium/web/formPage.html")
}
@Test
fun selectOption() {
val selectElement = driver.findElement(By.name("selectomatic"))
val select = Select(selectElement)
val twoElement = driver.findElement(By.cssSelector("option[value=two]"))
val fourElement = driver.findElement(By.cssSelector("option[value=four]"))
val countElement = driver.findElement(By.cssSelector("option[value='still learning how to count, apparently']"))
select.selectByVisibleText("Four")
Assertions.assertTrue(fourElement.isSelected())
select.selectByValue("two")
Assertions.assertTrue(twoElement.isSelected())
select.selectByIndex(3)
Assertions.assertTrue(countElement.isSelected())
}
@Test
fun selectMultipleOption() {
val selectElement = driver.findElement(By.name("multi"))
val select = Select(selectElement)
val hamElement = driver.findElement(By.cssSelector("option[value=ham]"))
val gravyElement = driver.findElement(By.cssSelector("option[value='onion gravy']"))
val eggElement = driver.findElement(By.cssSelector("option[value=eggs]"))
val sausageElement = driver.findElement(By.cssSelector("option[value='sausages']"))
val optionElements = selectElement.findElements(By.tagName("option"))
val optionList = select.getOptions()
Assertions.assertEquals(optionElements, optionList)
val selectedOptionList = select.getAllSelectedOptions()
val expectedSelection = ArrayList<WebElement>()
expectedSelection.add(eggElement)
expectedSelection.add(sausageElement)
Assertions.assertEquals(expectedSelection, selectedOptionList)
select.selectByValue("ham")
select.selectByValue("onion gravy")
Assertions.assertTrue(hamElement.isSelected())
Assertions.assertTrue(gravyElement.isSelected())
select.deselectByValue("eggs")
select.deselectByValue("sausages")
Assertions.assertFalse(eggElement.isSelected())
Assertions.assertFalse(sausageElement.isSelected())
}
@Test
fun disabledOption() {
val selectElement = driver.findElement(By.name("single_disabled"))
val select = Select(selectElement)
Assertions.assertThrows(UnsupportedOperationException::class.java) {
select.selectByValue("disabled")
}
}
}
Disabled options
Options with a disabled
attribute may not be selected.
<select name="single_disabled">
<option id="sinlge_disabled_1" value="enabled">Enabled</option>
<option id="sinlge_disabled_2" value="disabled" disabled="disabled">Disabled</option>
</select>
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
select.selectByValue("disabled");
});
Show full example
package dev.selenium.support;
import dev.selenium.BaseChromeTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import java.util.ArrayList;
import java.util.List;
public class SelectListTest extends BaseChromeTest {
@BeforeEach
public void navigate() {
driver.get("https://www.selenium.dev/selenium/web/formPage.html");
}
@Test
public void selectOption() {
WebElement selectElement = driver.findElement(By.name("selectomatic"));
Select select = new Select(selectElement);
WebElement twoElement = driver.findElement(By.cssSelector("option[value=two]"));
WebElement fourElement = driver.findElement(By.cssSelector("option[value=four]"));
WebElement countElement = driver.findElement(By.cssSelector("option[value='still learning how to count, apparently']"));
select.selectByVisibleText("Four");
Assertions.assertTrue(fourElement.isSelected());
select.selectByValue("two");
Assertions.assertTrue(twoElement.isSelected());
select.selectByIndex(3);
Assertions.assertTrue(countElement.isSelected());
}
@Test
public void selectMultipleOption() {
WebElement selectElement = driver.findElement(By.name("multi"));
Select select = new Select(selectElement);
WebElement hamElement = driver.findElement(By.cssSelector("option[value=ham]"));
WebElement gravyElement = driver.findElement(By.cssSelector("option[value='onion gravy']"));
WebElement eggElement = driver.findElement(By.cssSelector("option[value=eggs]"));
WebElement sausageElement = driver.findElement(By.cssSelector("option[value='sausages']"));
List<WebElement> optionElements = selectElement.findElements(By.tagName("option"));
List<WebElement> optionList = select.getOptions();
Assertions.assertEquals(optionElements, optionList);
List<WebElement> selectedOptionList = select.getAllSelectedOptions();
List<WebElement> expectedSelection = new ArrayList<WebElement>() {{
add(eggElement);
add(sausageElement);
}};
Assertions.assertEquals(expectedSelection, selectedOptionList);
select.selectByValue("ham");
select.selectByValue("onion gravy");
Assertions.assertTrue(hamElement.isSelected());
Assertions.assertTrue(gravyElement.isSelected());
select.deselectByValue("eggs");
select.deselectByValue("sausages");
Assertions.assertFalse(eggElement.isSelected());
Assertions.assertFalse(sausageElement.isSelected());
}
@Test
public void disabledOption() {
WebElement selectElement = driver.findElement(By.name("single_disabled"));
Select select = new Select(selectElement);
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
select.selectByValue("disabled");
});
}
}
with pytest.raises(NotImplementedError):
select.select_by_value('disabled')
Show full example
import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
def test_select_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'selectomatic')
select = Select(select_element)
two_element = driver.find_element(By.CSS_SELECTOR, 'option[value=two]')
four_element = driver.find_element(By.CSS_SELECTOR, 'option[value=four]')
count_element = driver.find_element(By.CSS_SELECTOR, "option[value='still learning how to count, apparently']")
select.select_by_visible_text('Four')
assert four_element.is_selected()
select.select_by_value('two')
assert two_element.is_selected()
select.select_by_index(3)
assert count_element.is_selected()
def test_select_multiple_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'multi')
select = Select(select_element)
ham_element = driver.find_element(By.CSS_SELECTOR, 'option[value=ham]')
gravy_element = driver.find_element(By.CSS_SELECTOR, "option[value='onion gravy']")
egg_element = driver.find_element(By.CSS_SELECTOR, 'option[value=eggs]')
sausage_element = driver.find_element(By.CSS_SELECTOR, "option[value='sausages']")
option_elements = select_element.find_elements(By.TAG_NAME, 'option')
option_list = select.options
assert option_elements == option_list
selected_option_list = select.all_selected_options
expected_selection = [egg_element, sausage_element]
assert selected_option_list == expected_selection
select.select_by_value('ham')
select.select_by_value('onion gravy')
assert ham_element.is_selected()
assert gravy_element.is_selected()
select.deselect_by_value('eggs')
select.deselect_by_value('sausages')
assert not egg_element.is_selected()
assert not sausage_element.is_selected()
def test_disabled_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'single_disabled')
select = Select(select_element)
with pytest.raises(NotImplementedError):
select.select_by_value('disabled')
Assert.ThrowsException<InvalidOperationException>(() => select.SelectByValue("disabled"));
Show full example
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace SeleniumDocs.Support
{
[TestClass]
public class SelectListTest : BaseChromeTest
{
[TestInitialize]
public void Navigate()
{
driver.Url = "https://www.selenium.dev/selenium/web/formPage.html";
}
[TestMethod]
public void SelectOption()
{
var selectElement = driver.FindElement(By.Name("selectomatic"));
var select = new SelectElement(selectElement);
var twoElement = driver.FindElement(By.CssSelector("option[value=two]"));
var fourElement = driver.FindElement(By.CssSelector("option[value=four]"));
var countElement = driver.FindElement(By.CssSelector("option[value='still learning how to count, apparently']"));
select.SelectByText("Four");
Assert.IsTrue(fourElement.Selected);
select.SelectByValue("two");
Assert.IsTrue(twoElement.Selected);
select.SelectByIndex(3);
Assert.IsTrue(countElement.Selected);
}
[TestMethod]
public void SelectMultipleOption()
{
var selectElement = driver.FindElement(By.Name("multi"));
var select = new SelectElement(selectElement);
var hamElement = driver.FindElement(By.CssSelector("option[value=ham]"));
var gravyElement = driver.FindElement(By.CssSelector("option[value='onion gravy']"));
var eggElement = driver.FindElement(By.CssSelector("option[value=eggs]"));
var sausageElement = driver.FindElement(By.CssSelector("option[value='sausages']"));
IList<IWebElement> optionList = select.Options;
IWebElement[] optionElements = selectElement.FindElements(By.TagName("option")).ToArray();
CollectionAssert.AreEqual(optionElements, optionList.ToArray());
IList<IWebElement> selectedOptionList = select.AllSelectedOptions;
IWebElement[] expectedSelection = { eggElement, sausageElement };
CollectionAssert.AreEqual(expectedSelection, selectedOptionList.ToArray());
select.SelectByValue("ham");
select.SelectByValue("onion gravy");
Assert.IsTrue(hamElement.Selected);
Assert.IsTrue(gravyElement.Selected);
select.DeselectByValue("eggs");
select.DeselectByValue("sausages");
Assert.IsFalse(eggElement.Selected);
Assert.IsFalse(sausageElement.Selected);
}
[TestMethod]
public void DisabledOption()
{
var selectElement = driver.FindElement(By.Name("single_disabled"));
var select = new SelectElement(selectElement);
Assert.ThrowsException<InvalidOperationException>(() => select.SelectByValue("disabled"));
}
}
}
expect {
select.select_by(:value, 'disabled')
}.to raise_exception(Selenium::WebDriver::Error::UnsupportedOperationError)
Show full example
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Select List' do
let(:driver) { start_session }
before do
driver.get('https://www.selenium.dev/selenium/web/formPage.html')
end
it 'select options' do
select_element = driver.find_element(name: 'selectomatic')
select = Selenium::WebDriver::Support::Select.new(select_element)
two_element = driver.find_element(css: 'option[value=two]')
four_element = driver.find_element(css: 'option[value=four]')
count_element = driver.find_element(css: "option[value='still learning how to count, apparently']")
select.select_by(:text, 'Four')
expect(four_element).to be_selected
select.select_by(:value, 'two')
expect(two_element).to be_selected
select.select_by(:index, 3)
expect(count_element).to be_selected
end
it 'select multiple options' do
select_element = driver.find_element(name: 'multi')
select = Selenium::WebDriver::Support::Select.new(select_element)
ham_element = driver.find_element(css: 'option[value=ham]')
gravy_element = driver.find_element(css: "option[value='onion gravy']")
egg_element = driver.find_element(css: 'option[value=eggs]')
sausage_element = driver.find_element(css: "option[value='sausages']")
option_elements = select_element.find_elements(tag_name: 'option')
option_list = select.options
expect(option_elements).to eq option_list
selected_option_list = select.selected_options
expected_selection = [egg_element, sausage_element]
expect(selected_option_list).to eq expected_selection
select.select_by(:value, 'ham')
select.select_by(:value, 'onion gravy')
expect(ham_element).to be_selected
expect(gravy_element).to be_selected
select.deselect_by(:value, 'eggs')
select.deselect_by(:value, 'sausages')
expect(egg_element).not_to be_selected
expect(sausage_element).not_to be_selected
end
it 'disabled options' do
select_element = driver.find_element(name: 'single_disabled')
select = Selenium::WebDriver::Support::Select.new(select_element)
expect {
select.select_by(:value, 'disabled')
}.to raise_exception(Selenium::WebDriver::Error::UnsupportedOperationError)
end
end
const select = await new Select(selectElement)
await assert.rejects(async () => {
await select.selectByValue("disabled")
Show full example
const {By, Browser, Builder} = require('selenium-webdriver')
const assert = require('assert/strict')
const {Select} = require('selenium-webdriver')
describe('Select Tests', async function () {
let driver
before(async function () {
driver = new Builder()
.forBrowser(Browser.FIREFOX)
.build()
await driver.get('https://www.selenium.dev/selenium/web/formPage.html')
})
after(async () => await driver.quit())
it('Select an option', async function () {
const selectElement = await driver.findElement(By.name('selectomatic'))
const select = new Select(selectElement)
const twoElement = await driver.findElement(By.css('option[value=two]'))
const fourElement = await driver.findElement(By.css('option[value=four]'))
const countElement = await driver.findElement(By.css("option[value='still learning how to count, apparently']"))
await select.selectByVisibleText('Four')
assert.equal(true, await fourElement.isSelected())
await select.selectByValue('two')
assert.equal(true, await twoElement.isSelected())
await select.selectByIndex(3)
assert.equal(true, await countElement.isSelected())
})
it('Select by multiple options', async function () {
const selectElement = await driver.findElement(By.name('multi'))
const select = await new Select(selectElement)
const hamElement = await driver.findElement(By.css('option[value=ham]'))
const gravyElement = await driver.findElement(By.css("option[value='onion gravy']"))
const eggElement = await driver.findElement(By.css('option[value=eggs]'))
const sausageElement = await driver.findElement(By.css("option[value='sausages']"))
const optionElements = await selectElement.findElements(By.css('option'))
const optionList = await select.getOptions()
assert.equal(optionList.length, optionElements.length)
for (const index in optionList) {
assert.equal(await optionList[index].getText(), await optionElements[index].getText())
}
const selectedOptionList = await select.getAllSelectedOptions()
const expectedSelection = [eggElement, sausageElement]
assert.equal(expectedSelection.length, selectedOptionList.length)
for (const index in selectedOptionList) {
assert.equal(await selectedOptionList[index].getText(), await expectedSelection[index].getText())
}
await select.selectByValue('ham')
await select.selectByValue('onion gravy')
assert.equal(true, await hamElement.isSelected())
assert.equal(true, await gravyElement.isSelected())
await select.deselectByValue('eggs')
await select.deselectByValue('sausages')
assert.equal(false, await eggElement.isSelected())
assert.equal(false, await sausageElement.isSelected())
})
it('Try selecting disabled option', async function () {
const selectElement = await driver.findElement(By.name('single_disabled'))
const select = await new Select(selectElement)
await assert.rejects(async () => {
await select.selectByValue("disabled")
}, {
name: 'UnsupportedOperationError',
message: 'You may not select a disabled option'
})
})
})
Assertions.assertThrows(UnsupportedOperationException::class.java) {
select.selectByValue("disabled")
}
Show full example
package dev.selenium.support
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.ui.Select
class SelectListTest : BaseTest() {
@BeforeEach
fun navigate() {
driver.get("https://www.selenium.dev/selenium/web/formPage.html")
}
@Test
fun selectOption() {
val selectElement = driver.findElement(By.name("selectomatic"))
val select = Select(selectElement)
val twoElement = driver.findElement(By.cssSelector("option[value=two]"))
val fourElement = driver.findElement(By.cssSelector("option[value=four]"))
val countElement = driver.findElement(By.cssSelector("option[value='still learning how to count, apparently']"))
select.selectByVisibleText("Four")
Assertions.assertTrue(fourElement.isSelected())
select.selectByValue("two")
Assertions.assertTrue(twoElement.isSelected())
select.selectByIndex(3)
Assertions.assertTrue(countElement.isSelected())
}
@Test
fun selectMultipleOption() {
val selectElement = driver.findElement(By.name("multi"))
val select = Select(selectElement)
val hamElement = driver.findElement(By.cssSelector("option[value=ham]"))
val gravyElement = driver.findElement(By.cssSelector("option[value='onion gravy']"))
val eggElement = driver.findElement(By.cssSelector("option[value=eggs]"))
val sausageElement = driver.findElement(By.cssSelector("option[value='sausages']"))
val optionElements = selectElement.findElements(By.tagName("option"))
val optionList = select.getOptions()
Assertions.assertEquals(optionElements, optionList)
val selectedOptionList = select.getAllSelectedOptions()
val expectedSelection = ArrayList<WebElement>()
expectedSelection.add(eggElement)
expectedSelection.add(sausageElement)
Assertions.assertEquals(expectedSelection, selectedOptionList)
select.selectByValue("ham")
select.selectByValue("onion gravy")
Assertions.assertTrue(hamElement.isSelected())
Assertions.assertTrue(gravyElement.isSelected())
select.deselectByValue("eggs")
select.deselectByValue("sausages")
Assertions.assertFalse(eggElement.isSelected())
Assertions.assertFalse(sausageElement.isSelected())
}
@Test
fun disabledOption() {
val selectElement = driver.findElement(By.name("single_disabled"))
val select = Select(selectElement)
Assertions.assertThrows(UnsupportedOperationException::class.java) {
select.selectByValue("disabled")
}
}
}
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.
select.deselectByValue("eggs");
Show full example
package dev.selenium.support;
import dev.selenium.BaseChromeTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import java.util.ArrayList;
import java.util.List;
public class SelectListTest extends BaseChromeTest {
@BeforeEach
public void navigate() {
driver.get("https://www.selenium.dev/selenium/web/formPage.html");
}
@Test
public void selectOption() {
WebElement selectElement = driver.findElement(By.name("selectomatic"));
Select select = new Select(selectElement);
WebElement twoElement = driver.findElement(By.cssSelector("option[value=two]"));
WebElement fourElement = driver.findElement(By.cssSelector("option[value=four]"));
WebElement countElement = driver.findElement(By.cssSelector("option[value='still learning how to count, apparently']"));
select.selectByVisibleText("Four");
Assertions.assertTrue(fourElement.isSelected());
select.selectByValue("two");
Assertions.assertTrue(twoElement.isSelected());
select.selectByIndex(3);
Assertions.assertTrue(countElement.isSelected());
}
@Test
public void selectMultipleOption() {
WebElement selectElement = driver.findElement(By.name("multi"));
Select select = new Select(selectElement);
WebElement hamElement = driver.findElement(By.cssSelector("option[value=ham]"));
WebElement gravyElement = driver.findElement(By.cssSelector("option[value='onion gravy']"));
WebElement eggElement = driver.findElement(By.cssSelector("option[value=eggs]"));
WebElement sausageElement = driver.findElement(By.cssSelector("option[value='sausages']"));
List<WebElement> optionElements = selectElement.findElements(By.tagName("option"));
List<WebElement> optionList = select.getOptions();
Assertions.assertEquals(optionElements, optionList);
List<WebElement> selectedOptionList = select.getAllSelectedOptions();
List<WebElement> expectedSelection = new ArrayList<WebElement>() {{
add(eggElement);
add(sausageElement);
}};
Assertions.assertEquals(expectedSelection, selectedOptionList);
select.selectByValue("ham");
select.selectByValue("onion gravy");
Assertions.assertTrue(hamElement.isSelected());
Assertions.assertTrue(gravyElement.isSelected());
select.deselectByValue("eggs");
select.deselectByValue("sausages");
Assertions.assertFalse(eggElement.isSelected());
Assertions.assertFalse(sausageElement.isSelected());
}
@Test
public void disabledOption() {
WebElement selectElement = driver.findElement(By.name("single_disabled"));
Select select = new Select(selectElement);
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
select.selectByValue("disabled");
});
}
}
select.deselect_by_value('eggs')
Show full example
import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
def test_select_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'selectomatic')
select = Select(select_element)
two_element = driver.find_element(By.CSS_SELECTOR, 'option[value=two]')
four_element = driver.find_element(By.CSS_SELECTOR, 'option[value=four]')
count_element = driver.find_element(By.CSS_SELECTOR, "option[value='still learning how to count, apparently']")
select.select_by_visible_text('Four')
assert four_element.is_selected()
select.select_by_value('two')
assert two_element.is_selected()
select.select_by_index(3)
assert count_element.is_selected()
def test_select_multiple_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'multi')
select = Select(select_element)
ham_element = driver.find_element(By.CSS_SELECTOR, 'option[value=ham]')
gravy_element = driver.find_element(By.CSS_SELECTOR, "option[value='onion gravy']")
egg_element = driver.find_element(By.CSS_SELECTOR, 'option[value=eggs]')
sausage_element = driver.find_element(By.CSS_SELECTOR, "option[value='sausages']")
option_elements = select_element.find_elements(By.TAG_NAME, 'option')
option_list = select.options
assert option_elements == option_list
selected_option_list = select.all_selected_options
expected_selection = [egg_element, sausage_element]
assert selected_option_list == expected_selection
select.select_by_value('ham')
select.select_by_value('onion gravy')
assert ham_element.is_selected()
assert gravy_element.is_selected()
select.deselect_by_value('eggs')
select.deselect_by_value('sausages')
assert not egg_element.is_selected()
assert not sausage_element.is_selected()
def test_disabled_options(driver):
driver.get('https://selenium.dev/selenium/web/formPage.html')
select_element = driver.find_element(By.NAME, 'single_disabled')
select = Select(select_element)
with pytest.raises(NotImplementedError):
select.select_by_value('disabled')
select.DeselectByValue("eggs");
Show full example
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace SeleniumDocs.Support
{
[TestClass]
public class SelectListTest : BaseChromeTest
{
[TestInitialize]
public void Navigate()
{
driver.Url = "https://www.selenium.dev/selenium/web/formPage.html";
}
[TestMethod]
public void SelectOption()
{
var selectElement = driver.FindElement(By.Name("selectomatic"));
var select = new SelectElement(selectElement);
var twoElement = driver.FindElement(By.CssSelector("option[value=two]"));
var fourElement = driver.FindElement(By.CssSelector("option[value=four]"));
var countElement = driver.FindElement(By.CssSelector("option[value='still learning how to count, apparently']"));
select.SelectByText("Four");
Assert.IsTrue(fourElement.Selected);
select.SelectByValue("two");
Assert.IsTrue(twoElement.Selected);
select.SelectByIndex(3);
Assert.IsTrue(countElement.Selected);
}
[TestMethod]
public void SelectMultipleOption()
{
var selectElement = driver.FindElement(By.Name("multi"));
var select = new SelectElement(selectElement);
var hamElement = driver.FindElement(By.CssSelector("option[value=ham]"));
var gravyElement = driver.FindElement(By.CssSelector("option[value='onion gravy']"));
var eggElement = driver.FindElement(By.CssSelector("option[value=eggs]"));
var sausageElement = driver.FindElement(By.CssSelector("option[value='sausages']"));
IList<IWebElement> optionList = select.Options;
IWebElement[] optionElements = selectElement.FindElements(By.TagName("option")).ToArray();
CollectionAssert.AreEqual(optionElements, optionList.ToArray());
IList<IWebElement> selectedOptionList = select.AllSelectedOptions;
IWebElement[] expectedSelection = { eggElement, sausageElement };
CollectionAssert.AreEqual(expectedSelection, selectedOptionList.ToArray());
select.SelectByValue("ham");
select.SelectByValue("onion gravy");
Assert.IsTrue(hamElement.Selected);
Assert.IsTrue(gravyElement.Selected);
select.DeselectByValue("eggs");
select.DeselectByValue("sausages");
Assert.IsFalse(eggElement.Selected);
Assert.IsFalse(sausageElement.Selected);
}
[TestMethod]
public void DisabledOption()
{
var selectElement = driver.FindElement(By.Name("single_disabled"));
var select = new SelectElement(selectElement);
Assert.ThrowsException<InvalidOperationException>(() => select.SelectByValue("disabled"));
}
}
}
select.deselect_by(:value, 'eggs')
Show full example
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Select List' do
let(:driver) { start_session }
before do
driver.get('https://www.selenium.dev/selenium/web/formPage.html')
end
it 'select options' do
select_element = driver.find_element(name: 'selectomatic')
select = Selenium::WebDriver::Support::Select.new(select_element)
two_element = driver.find_element(css: 'option[value=two]')
four_element = driver.find_element(css: 'option[value=four]')
count_element = driver.find_element(css: "option[value='still learning how to count, apparently']")
select.select_by(:text, 'Four')
expect(four_element).to be_selected
select.select_by(:value, 'two')
expect(two_element).to be_selected
select.select_by(:index, 3)
expect(count_element).to be_selected
end
it 'select multiple options' do
select_element = driver.find_element(name: 'multi')
select = Selenium::WebDriver::Support::Select.new(select_element)
ham_element = driver.find_element(css: 'option[value=ham]')
gravy_element = driver.find_element(css: "option[value='onion gravy']")
egg_element = driver.find_element(css: 'option[value=eggs]')
sausage_element = driver.find_element(css: "option[value='sausages']")
option_elements = select_element.find_elements(tag_name: 'option')
option_list = select.options
expect(option_elements).to eq option_list
selected_option_list = select.selected_options
expected_selection = [egg_element, sausage_element]
expect(selected_option_list).to eq expected_selection
select.select_by(:value, 'ham')
select.select_by(:value, 'onion gravy')
expect(ham_element).to be_selected
expect(gravy_element).to be_selected
select.deselect_by(:value, 'eggs')
select.deselect_by(:value, 'sausages')
expect(egg_element).not_to be_selected
expect(sausage_element).not_to be_selected
end
it 'disabled options' do
select_element = driver.find_element(name: 'single_disabled')
select = Selenium::WebDriver::Support::Select.new(select_element)
expect {
select.select_by(:value, 'disabled')
}.to raise_exception(Selenium::WebDriver::Error::UnsupportedOperationError)
end
end
assert.equal(true, await gravyElement.isSelected())
Show full example
const {By, Browser, Builder} = require('selenium-webdriver')
const assert = require('assert/strict')
const {Select} = require('selenium-webdriver')
describe('Select Tests', async function () {
let driver
before(async function () {
driver = new Builder()
.forBrowser(Browser.FIREFOX)
.build()
await driver.get('https://www.selenium.dev/selenium/web/formPage.html')
})
after(async () => await driver.quit())
it('Select an option', async function () {
const selectElement = await driver.findElement(By.name('selectomatic'))
const select = new Select(selectElement)
const twoElement = await driver.findElement(By.css('option[value=two]'))
const fourElement = await driver.findElement(By.css('option[value=four]'))
const countElement = await driver.findElement(By.css("option[value='still learning how to count, apparently']"))
await select.selectByVisibleText('Four')
assert.equal(true, await fourElement.isSelected())
await select.selectByValue('two')
assert.equal(true, await twoElement.isSelected())
await select.selectByIndex(3)
assert.equal(true, await countElement.isSelected())
})
it('Select by multiple options', async function () {
const selectElement = await driver.findElement(By.name('multi'))
const select = await new Select(selectElement)
const hamElement = await driver.findElement(By.css('option[value=ham]'))
const gravyElement = await driver.findElement(By.css("option[value='onion gravy']"))
const eggElement = await driver.findElement(By.css('option[value=eggs]'))
const sausageElement = await driver.findElement(By.css("option[value='sausages']"))
const optionElements = await selectElement.findElements(By.css('option'))
const optionList = await select.getOptions()
assert.equal(optionList.length, optionElements.length)
for (const index in optionList) {
assert.equal(await optionList[index].getText(), await optionElements[index].getText())
}
const selectedOptionList = await select.getAllSelectedOptions()
const expectedSelection = [eggElement, sausageElement]
assert.equal(expectedSelection.length, selectedOptionList.length)
for (const index in selectedOptionList) {
assert.equal(await selectedOptionList[index].getText(), await expectedSelection[index].getText())
}
await select.selectByValue('ham')
await select.selectByValue('onion gravy')
assert.equal(true, await hamElement.isSelected())
assert.equal(true, await gravyElement.isSelected())
await select.deselectByValue('eggs')
await select.deselectByValue('sausages')
assert.equal(false, await eggElement.isSelected())
assert.equal(false, await sausageElement.isSelected())
})
it('Try selecting disabled option', async function () {
const selectElement = await driver.findElement(By.name('single_disabled'))
const select = await new Select(selectElement)
await assert.rejects(async () => {
await select.selectByValue("disabled")
}, {
name: 'UnsupportedOperationError',
message: 'You may not select a disabled option'
})
})
})
select.deselectByValue("eggs")
Show full example
package dev.selenium.support
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.ui.Select
class SelectListTest : BaseTest() {
@BeforeEach
fun navigate() {
driver.get("https://www.selenium.dev/selenium/web/formPage.html")
}
@Test
fun selectOption() {
val selectElement = driver.findElement(By.name("selectomatic"))
val select = Select(selectElement)
val twoElement = driver.findElement(By.cssSelector("option[value=two]"))
val fourElement = driver.findElement(By.cssSelector("option[value=four]"))
val countElement = driver.findElement(By.cssSelector("option[value='still learning how to count, apparently']"))
select.selectByVisibleText("Four")
Assertions.assertTrue(fourElement.isSelected())
select.selectByValue("two")
Assertions.assertTrue(twoElement.isSelected())
select.selectByIndex(3)
Assertions.assertTrue(countElement.isSelected())
}
@Test
fun selectMultipleOption() {
val selectElement = driver.findElement(By.name("multi"))
val select = Select(selectElement)
val hamElement = driver.findElement(By.cssSelector("option[value=ham]"))
val gravyElement = driver.findElement(By.cssSelector("option[value='onion gravy']"))
val eggElement = driver.findElement(By.cssSelector("option[value=eggs]"))
val sausageElement = driver.findElement(By.cssSelector("option[value='sausages']"))
val optionElements = selectElement.findElements(By.tagName("option"))
val optionList = select.getOptions()
Assertions.assertEquals(optionElements, optionList)
val selectedOptionList = select.getAllSelectedOptions()
val expectedSelection = ArrayList<WebElement>()
expectedSelection.add(eggElement)
expectedSelection.add(sausageElement)
Assertions.assertEquals(expectedSelection, selectedOptionList)
select.selectByValue("ham")
select.selectByValue("onion gravy")
Assertions.assertTrue(hamElement.isSelected())
Assertions.assertTrue(gravyElement.isSelected())
select.deselectByValue("eggs")
select.deselectByValue("sausages")
Assertions.assertFalse(eggElement.isSelected())
Assertions.assertFalse(sausageElement.isSelected())
}
@Test
fun disabledOption() {
val selectElement = driver.findElement(By.name("single_disabled"))
val select = Select(selectElement)
Assertions.assertThrows(UnsupportedOperationException::class.java) {
select.selectByValue("disabled")
}
}
}
5 - ThreadGuard
This class is only available in the Java Binding
ThreadGuard checks that a driver is called only from the same thread that created it. Threading issues especially when running tests in Parallel may have mysterious and hard to diagnose errors. Using this wrapper prevents this category of errors and will raise an exception when it happens.
The following example simulate a clash of threads:
public class DriverClash {
//thread main (id 1) created this driver
private WebDriver protectedDriver = ThreadGuard.protect(new ChromeDriver());
static {
System.setProperty("webdriver.chrome.driver", "<Set path to your Chromedriver>");
}
//Thread-1 (id 24) is calling the same driver causing the clash to happen
Runnable r1 = () -> {protectedDriver.get("https://selenium.dev");};
Thread thr1 = new Thread(r1);
void runThreads(){
thr1.start();
}
public static void main(String[] args) {
new DriverClash().runThreads();
}
}
The result shown below:
Exception in thread "Thread-1" org.openqa.selenium.WebDriverException:
Thread safety error; this instance of WebDriver was constructed
on thread main (id 1)and is being accessed by thread Thread-1 (id 24)
This is not permitted and *will* cause undefined behaviour
As seen in the example:
protectedDriver
Will be created in Main thread- We use Java
Runnable
to spin up a new process and a newThread
to run the process - Both
Thread
will clash because the Main Thread does not haveprotectedDriver
in it’s memory. ThreadGuard.protect
will throw an exception.
Note:
This does not replace the need for using ThreadLocal
to manage drivers when running parallel.