In HTML, when a user required to select a input from given list of options, select box is used. It's an element that has a drop-down list of options. The user can select required options from the list. There are two types of select boxes ie single select to select single user input and multi select for multiple user inputs.
Below are the HTML code for single select box.
<select name="oldCars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Below are the HTML code for multi select box.
<select name="cars" multiple="" class="multipleCombo">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
<option value="Merc">Merc</option>
<option value="bmw">BMW</option>
</select>
Following are the different commands to interact with the select box.
Sometimes we need to check if the select box element support selecting multiple options or just a single options at a time. This is done by checking the value of the "multiple" attribute. If this method returns true, it support multiple selection of options.
To work with select box we need to create an object of the Select class by passing the web element of the select box. Lets look at the code for select box.
Java
C#
Python
Javascript
Kotlin
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/getData/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("oldCars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//Check if select box is multiple select
boolean isMultipleSelect = dropdownObject.isMultiple();
IWebDriver driver = new ChromeDriver();
driver.Url = "http://localhost:8080/demo/getData/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("oldCars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//Check if select box is multiple select
bool isMultipleSelect = dropdownObject.IsMultiple;
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("http://localhost:8080/demo/getData/")
select_elem = driver.find_element(By.NAME, "oldCars")
# Create select box object
dropdown_object = Select(select_elem)
# Check if select box is multiple select
is_multiple_select = dropdown_object.is_multiple()
print(is_multiple_select)
driver.quit()
let driver = await new Builder().forBrowser('chrome').build();
await driver.get("http://localhost:8080/demo/getData/");
await driver.manage().setTimeouts({ implicit: 30000 });
// Find the select box element
let selectBoxelem = await driver.findElement(By.name("cars"));
//Create select box object
let isMultiple = await selectBoxelem.getAttribute("multiple");
if (isMultiple !== null)
{
console.log("The element has the 'multiple' attribute.");
}
else
{
console.log("The element does not have the 'multiple' attribute.");
}
await driver.quit();
val driver: WebDriver = ChromeDriver()
driver["http://localhost:8080/demo/getData/"]
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30))
// Find the select box element
val selectBoxelem = driver.findElement(By.name("cars"))
//Create select box object
val dropdownObject: Select = Select(selectBoxelem)
//Check if select box is multiple select
val isMultipleSelect: Boolean = dropdownObject.isMultiple
println(isMultipleSelect)
driver.quit()
The method selectByVisibleText will select all the option available in the list that displayed with text matching to the given argument. In the following example we have given argument as Opel, this would select an option having inner text equals to Opel. It will throw NoSuchElementException error, if no matching option elements are found.
Java
C#
Python
Javascript
Kotlin
WebDriver driver = new ChromeDriver();
driver.get("http://localhost/demo/setData/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("oldCars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//Select option which is visible in the list
dropdownObject.selectByVisibleText("Opel");
driver.quit();
IWebDriver driver = new ChromeDriver();
driver.Url = "http://localhost/demo/setData/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("oldCars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//Select option which is visible in the list
dropdownObject.SelectByText("Opel");
driver.Quit();
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("http://localhost:8080/demo/setData/")
select_elem = driver.find_element(By.NAME, "oldCars")
# Create select box object
dropdown_object = Select(select_elem)
# Select option which is visible in the list
dropdown_object.select_by_visible_text("Opel")
driver.quit()
let driver = await new Builder().forBrowser('chrome').build();
await driver.get("http://localhost:8080/demo/getData/");
await driver.manage().setTimeouts({ implicit: 30000 });
// Find the select box element
let selectBoxelem = await driver.findElement(By.name("cars"));
//Create select box object
let dropdownObject = new Select(selectBoxelem);
await dropdownObject .selectByVisibleText("Volvo");
await driver.quit();
val driver: WebDriver = ChromeDriver()
driver["http://localhost/demo/setData/"]
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30))
// Find the select box element
val selectBoxelem = driver.findElement(By.name("oldCars"))
//Create select box object
val dropdownObject = Select(selectBoxelem)
//Select option which is visible in the list
dropdownObject.selectByVisibleText("Opel")
driver.quit()
The method selectByIndex will select the option at the given index. This is done by examining the index attribute of an element. The index starts from 0. It will throw NoSuchElementException error, if no matching option elements are found.
Java
C#
Python
Javascript
Kotlin
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/setData/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("oldCars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//Select option by its index. At index 0 we have Volvo.
dropdownObject.selectByIndex(0);
driver.quit();
IWebDriver driver = new ChromeDriver();
driver.Url = "http://localhost:8080/demo/setData/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("oldCars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//Select option by its index. At index 0 we have Volvo.
dropdownObject.SelectByIndex(0);
driver.Quit();
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("http://localhost:8080/demo/setData/")
select_elem = driver.find_element(By.NAME, "oldCars")
# Create select box object
dropdown_object = Select(select_elem)
# Select option with index 1
dropdown_object.select_by_index(1)
driver.quit()
let driver = await new Builder().forBrowser('chrome').build();
await driver.get("http://localhost:8080/demo/get/");
await driver.manage().setTimeouts({ implicit: 30000 });
// Find the select box element
let selectBoxelem = await driver.findElement(By.name("cars"));
//Create select box object
let dropdownObject = new Select(selectBoxelem);
// Select the option at the given index 2.
await dropdownObject .selectByIndex(2);
await driver.quit();
val driver: WebDriver = ChromeDriver()
driver["http://localhost/demo/setData/"]
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30))
// Find the select box element
val selectBoxelem = driver.findElement(By.name("oldCars"))
//Create select box object
val dropdownObject = Select(selectBoxelem)
//Select option by index
dropdownObject.selectByIndex(2)
driver.quit()
The method selectByValue will select all the option that have a value matching the given argument. In the following example we have given argument as saab, this would select an option with value equals to saab ie option Saab. It will throw NoSuchElementException error, if no matching option elements are found.
Java
C#
Python
Javascript
Kotlin
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/setData/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("oldCars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//Select option by its value
dropdownObject.selectByValue("saab");
driver.quit();
IWebDriver driver = new ChromeDriver();
driver.Url = "http://localhost:8080/demo/setData/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("oldCars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//Select option by its value
dropdownObject.SelectByValue("saab");
driver.Quit();
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("http://localhost:8080/demo/setData/")
select_elem = driver.find_element(By.NAME, "oldCars")
# Create select box object
dropdown_object = Select(select_elem)
# Select option by its value
dropdown_object.select_by_value("saab")
driver.quit()
let driver = await new Builder().forBrowser('chrome').build();
await driver.get("http://localhost:8080/demo/getData/");
await driver.manage().setTimeouts({ implicit: 30000 });
// Find the select box element
let selectBoxelem = await driver.findElement(By.name("cars"));
//Create select box object
let dropdownObject = new Select(selectBoxelem);
//Select option by its value
await dropdownObject .selectByValue("saab");
await driver.quit();
val driver: WebDriver = ChromeDriver()
driver["http://localhost/demo/setData/"]
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30))
// Find the select box element
val selectBoxelem = driver.findElement(By.name("oldCars"))
//Create select box object
val dropdownObject = Select(selectBoxelem)
//Select option by value
dropdownObject.selectByValue("saab")
driver.quit()
This method will return all options belonging to this select tag. The return type will be list of web elements.
Java
C#
Python
Javascript
Kotlin
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/setData/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("oldCars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//To get all options in select box
List<WebElement> optionsList = dropdownObject.getOptions();
for (WebElement option : optionsList )
{
System.out.println(option.getText());
}
driver.quit();
IWebDriver driver = new ChromeDriver();
driver.Url = "http://localhost:8080/demo/setData/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("oldCars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//To get all options in select box
IList<IWebElement> optionsList = dropdownObject.Options;
foreach (IWebElement option in optionsList)
{
Console.WriteLine(option.Text);
}
driver.Quit();
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("http://localhost:8080/demo/setData/")
select_elem = driver.find_element(By.NAME, "oldCars")
# Create select box object
dropdown_object = Select(select_elem)
# To get all options in select box
all_options = dropdown_object.options
for option in all_options:
print(option.text)
driver.quit()
let driver = await new Builder().forBrowser('chrome').build();
await driver.get("http://localhost:8080/demo/getData/");
await driver.manage().setTimeouts({ implicit: 30000 });
// Find the select box element
let selectBoxelem = await driver.findElement(By.name("cars"));
//Create select box object
let dropdownObject = new Select(selectBoxelem);
//To get all options in select box
for (let option of await dropdownObject.getOptions())
{
let optionText = await option.getText();
console.log("Option Text: " + optionText);
}
await driver.quit();
val driver: WebDriver = ChromeDriver()
driver["http://localhost/demo/setData/"]
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30))
// Find the select box element
val selectBoxelem = driver.findElement(By.name("oldCars"))
// Create select box object
val dropdownObject = Select(selectBoxelem)
// To get all options in select box
dropdownObject.options.forEach{ option->
println(option.text)
}
driver.quit()
This method will return all selected options belonging to this select tag. The return type will be list of web elements.
Java
C#
Python
Javascript
Kotlin
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/setData/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("oldCars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//To get all the selected options in select box
List<WebElement> selectedOptionList = dropdownObject.getAllSelectedOptions();
for (WebElement option : selectedOptionList )
{
System.out.println(option.getText());
}
driver.quit()
IWebDriver driver = new ChromeDriver();
driver.Url = "http://localhost:8080/demo/setData/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("oldCars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//To get all selected options in select box
IList<IWebElement> selectedOptionList = dropdownObject.AllSelectedOptions;
foreach (IWebElement option in selectedOptionList)
{
Console.WriteLine(option.Text);
}
driver.Quit();
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("http://localhost:8080/demo/setData/")
select_elem = driver.find_element(By.NAME, "oldCars")
# Create select box object
dropdown_object = Select(select_elem)
# To get all selected options in select
all_selected_options = dropdown_object.all_selected_options
for option in all_selected_options:
print(option.text)
driver.quit()
let driver = await new Builder().forBrowser('chrome').build();
await driver.get("http://localhost:8080/demo/getData/");
await driver.manage().setTimeouts({ implicit: 30000 });
// Find the select box element
let selectBoxelem = await driver.findElement(By.name("cars"));
//Create select box object
let dropdownObject = new Select(selectBoxelem);
// Get all currently selected options
let selectedOptions = await dropdownObject.getAllSelectedOptions();
// Print the text of each selected option
for (let option of selectedOptions) {
console.log(await option.getText());
}
await driver.quit();
val driver: WebDriver = ChromeDriver()
driver["http://localhost/demo/setData/"]
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30))
// Find the select box element
val selectBoxelem = driver.findElement(By.name("oldCars"))
// Create select box object
val dropdownObject = Select(selectBoxelem)
// To get all selected options in select box
dropdownObject.allSelectedOptions.forEach{ option->
println(option.text)
}
driver.quit()
This method will return the first selected option in this select tag if it is a multi select box else currently selected option in a normal select.
Java
C#
Python
Javascript
Kotlin
WebDriver driver = new ChromeDriver();
driver.get("http://localhost/demo/setData/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("oldCars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//To get first selected option in select box
WebElement firstSelectedOption = dropdownObject.getFirstSelectedOption();
System.out.println(firstSelectedOption.getText());
driver.quit()
IWebDriver driver = new ChromeDriver();
driver.Url = "http://localhost:8080/demo/setData/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("oldCars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//To get first selected options in select box
IWebElement firstSelectedOptionList = dropdownObject.SelectedOption;
Console.WriteLine(firstSelectedOptionList.Text);
driver.Quit();
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("http://localhost:8080/demo/setData/")
select_elem = driver.find_element(By.NAME, "oldCars")
# Create select box object
dropdown_object = Select(select_elem)
# To get first selected option in select
first_selected_options = dropdown_object.first_selected_option
print(first_selected_options)
driver.quit()
let driver = await new Builder().forBrowser('chrome').build();
await driver.get("http://localhost:8080/demo/getData/");
await driver.manage().setTimeouts({ implicit: 30000 });
// Find the select box element
let selectBoxelem = await driver.findElement(By.name("cars"));
//Create select box object
let dropdownObject = new Select(selectBoxelem);
// Get the first selected option
let firstSelectedOption = await dropdownObject.getFirstSelectedOption();
// Print the text of the first selected option
console.log(await firstSelectedOption.getText());
await driver.quit();
val driver: WebDriver = ChromeDriver()
driver["http://localhost/demo/setData/"]
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30))
// Find the select box element
val selectBoxelem = driver.findElement(By.name("oldCars"))
// Create select box object
val dropdownObject = Select(selectBoxelem)
// To get first selected option in select box
println(dropdownObject.firstSelectedOption)
driver.quit()
This method will clear all selected options. This is only valid when the SELECT supports multiple selections.
Java
C#
Python
Javascript
Kotlin
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/setData/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("cars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//To deselect all options
dropdownObject.deselectAll();
driver.quit();
IWebDriver driver = new ChromeDriver();
driver.Url = "http://localhost:8080/demo/setData/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("cars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//To deselect all options
dropdownObject.DeselectAll();
driver.Quit();
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("http://localhost:8080/demo/setData/")
select_elem = driver.find_element(By.NAME, "oldCars")
# Create select box object
dropdown_object = Select(select_elem)
# To deselect all options
dropdown_object.deselect_all()
driver.quit()
let driver = await new Builder().forBrowser('chrome').build();
await driver.get("http://localhost:8080/demo/getData/");
await driver.manage().setTimeouts({ implicit: 30000 });
// Find the select box element
let selectBoxelem = await driver.findElement(By.name("cars"));
//Create select box object
let dropdownObject = new Select(selectBoxelem);
//To deselect all options
dropdownObject.deselectAll();
await driver.quit();
val driver: WebDriver = ChromeDriver()
driver["http://localhost/demo/setData/"]
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30))
// Find the select box element
val selectBoxelem = driver.findElement(By.name("oldCars"))
// Create select box object
val dropdownObject = Select(selectBoxelem)
//To deselect all options
dropdownObject.deselectAll()
driver.quit()
The method deselectByVisibleText will deselect all the option available in the list that displayed with text matching to the given argument. This is only valid when the SELECT supports multiple selections. In the following example we have given argument as Opel, this would select an option having inner text equals to Opel. It will throw NoSuchElementException error, if no matching option elements are found.
Java
C#
Python
Javascript
Kotlin
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/setData/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("cars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//Select option which is visible in the list
dropdownObject.selectByVisibleText("Opel");
Thread.sleep(2000);
//Deselect option which is visible in the list
dropdownObject.deselectByVisibleText("Opel");
driver.quit();
IWebDriver driver = new ChromeDriver();
driver.Url = "http://localhost:8080/demo/setData/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("cars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//Select option which is visible in the list
dropdownObject.SelectByText("Opel");
Thread.Sleep(2000);
//Deselect option which is visible in the list
dropdownObject.DeselectByText("Opel");
driver.Quit();
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("http://localhost:8080/demo/setData/")
select_elem = driver.find_element(By.NAME, "oldCars")
# Create select box object
dropdown_object = Select(select_elem)
# Deselect option which is visible in the list
dropdown_object.deselect_by_visible_text("Opel")
driver.quit()
let driver = await new Builder().forBrowser('chrome').build();
await driver.get("http://localhost:8080/demo/getData/");
await driver.manage().setTimeouts({ implicit: 30000 });
// Find the select box element
let selectBoxelem = await driver.findElement(By.name("cars"));
//Create select box object
let dropdownObject = new Select(selectBoxelem);
//Deselect option which is visible in the list
dropdownObject.deselectByVisibleText("Opel");
await driver.quit();
val driver: WebDriver = ChromeDriver()
driver["http://localhost/demo/setData/"]
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30))
// Find the select box element
val selectBoxelem = driver.findElement(By.name("oldCars"))
// Create select box object
val dropdownObject = Select(selectBoxelem)
//Deselect option which is visible in the list
dropdownObject.deselectByVisibleText("Opel")
driver.quit()
The method deselectByVisibleText will deselect the option at the given index. This is done by examining the index attribute of an element. The index starts from 0. This is only valid when the SELECT supports multiple selections. It will throw NoSuchElementException error, if no matching option elements are found.
Java
C#
Python
Javascript
Kotlin
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/setData/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("cars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//Select option by its index. At index 0 we have Volvo.
dropdownObject.selectByIndex(0);
Thread.sleep(2000);
//Deselect option by its index. At index 0 we have Volvo.
dropdownObject.deselectByIndex(0);
driver.quit()
IWebDriver driver = new ChromeDriver();
driver.Url = "http://localhost:8080/demo/setData/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("cars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//Select option by its index. At index 0 we have Volvo.
dropdownObject.SelectByIndex(0);
Thread.Sleep(2000);
//Deselect option by its index. At index 0 we have Volvo.
dropdownObject.DeselectByIndex(0);
driver.Quit();
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("http://localhost:8080/demo/setData/")
select_elem = driver.find_element(By.NAME, "oldCars")
# Create select box object
dropdown_object = Select(select_elem)
# Select option by its index. At index 0 we have Volvo.
dropdown_object.select_by_index(0)
time.sleep(2000)
# Deselect option by its index. At index 0 we have Volvo.
dropdown_object.deselect_by_index(0)
driver.quit()
let driver = await new Builder().forBrowser('chrome').build();
await driver.get("http://localhost:8080/demo/getData/");
await driver.manage().setTimeouts({ implicit: 30000 });
// Find the select box element
let selectBoxelem = await driver.findElement(By.name("cars"));
//Create select box object
let dropdownObject = new Select(selectBoxelem);
//Select option by its index. At index 0 we have Volvo.
dropdownObject.SelectByIndex(0);
//Deselect option by its index. At index 0 we have Volvo.
dropdownObject.DeselectByIndex(0);
await driver.quit();
val driver: WebDriver = ChromeDriver()
driver["http://localhost/demo/setData/"]
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30))
// Find the select box element
val selectBoxelem = driver.findElement(By.name("oldCars"))
// Create select box object
val dropdownObject = Select(selectBoxelem)
//Select option by its index. At index 0 we have Volvo.
dropdownObject.selectByIndex(0);
Thread.sleep(2000);
//Deselect option by its index. At index 0 we have Volvo.
dropdownObject.deselectByIndex(0);
driver.quit()
The method deselectByValue will deselect all the option that have a value matching the given argument. This is only valid when the SELECT supports multiple selections. In the following example we have given argument as saab, this would deselect an option with value equals to saab ie option Saab. It will throw NoSuchElementException error, if no matching option elements are found.
Java
C#
Python
Javascript
Kotlin
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/setData/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("cars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//Select option by its value
dropdownObject.selectByValue("saab");
Thread.sleep(2000);
//Deselect option by its value
dropdownObject.deselectByValue("saab");
driver.quit();
IWebDriver driver = new ChromeDriver();
driver.Url = "http://localhost:8080/demo/setData/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("cars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//Select option by its value
dropdownObject.SelectByValue("saab");
Thread.Sleep(2000);
//Deselect option by its value
dropdownObject.DeselectByValue("saab");
driver.Quit();
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("http://localhost:8080/demo/setData/")
select_elem = driver.find_element(By.NAME, "oldCars")
# Create select box object
dropdown_object = Select(select_elem)
# Select option by its value
dropdown_object.select_by_value("saab")
time.sleep(2000)
# Deselect option by its value
dropdown_object.deselect_by_value("saab")
driver.quit()
let driver = await new Builder().forBrowser('chrome').build();
await driver.get("http://localhost:8080/demo/getData/");
await driver.manage().setTimeouts({ implicit: 30000 });
// Find the select box element
let selectBoxelem = await driver.findElement(By.name("cars"));
//Create select box object
let dropdownObject = new Select(selectBoxelem);
//Select option by its value
dropdownObject.SelectByValue("saab");
Thread.Sleep(2000);
//Deselect option by its value
dropdownObject.DeselectByValue("saab");
await driver.quit();
val driver: WebDriver = ChromeDriver()
driver["http://localhost/demo/setData/"]
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30))
// Find the select box element
val selectBoxelem = driver.findElement(By.name("oldCars"))
// Create select box object
val dropdownObject = Select(selectBoxelem)
//Select option by its value
dropdownObject.selectByValue("saab")
Thread.sleep(2000);
//Deselect option by its value
dropdownObject.deselectByValue("saab")
driver.quit()