選択要素の操作

選択リストには、他の要素と比較して特別な動作があります。

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

Selenium v4.5

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")
    }
  }
}
最終更新 November 17, 2023: Upgrade to Docsy 0 7 2 (#1529) (48f4361690)