Control a selectbox or dropdownbox with Selenium WebDriver

5 (100%) 2 votes

Problem

We want to select and deselect selectbox options with Selenium WebDriver.

Solution

The example code below shows the variety of methods we can use to control a selectbox. We can also determine if we have to control a multi-selectbox or what the first selected element is.

<code lang="java">
package seleniumWebDriverAPI;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
 * The Class SelectExample.
 */
public class SelectExample {

    /** The driver. */
    private static WebDriver driver;

    /**
     * Open browser
     */
    @BeforeClass
    public void setUp() {
        driver = new FirefoxDriver();
        driver.get("http://techblog.polteq.com/prestashop/contact-form.php");
    }

    /**
     * Tear down.
     */
    @AfterClass
    public void tearDown() {
        driver.close();
        driver.quit();
    }

    /**
     * Clear all selected entries. This is only valid when the SELECT supports
     * multiple selections.
     */
    @Test
    public void deselectAll() {
        Select selectBox = new Select(driver.findElement(By
                .cssSelector("select#id_contact")));
        selectBox.deselectAll();
    }

    /**
     * Deselect the option at the given index. This is done by examing the
     * "index" attribute of an element, and not merely by counting.
     */
    @Test
    public void deselectByIndex() {
        Select selectBox = new Select(driver.findElement(By
                .cssSelector("select#id_contact")));
        selectBox.deselectByIndex(2);
    }

    /**
     * Deselect all options that have a value matching the argument.
     */
    @Test
    public void deselectByValue() {
        Select selectBox = new Select(driver.findElement(By
                .cssSelector("select#id_contact")));
        selectBox.deselectByValue("2");
    }

    /**
     * Deselect all options that display text matching the argument.
     */
    @Test
    public void deselectByVisibleText() {
        Select selectBox = new Select(driver.findElement(By
                .cssSelector("select#id_contact")));
        selectBox.deselectByVisibleText("Customer service");
    }

    /**
     * Gets all selected options belonging to this select tag.
     */
    @Test
    public void getAllSelectedOptions() {
        Select selectBox = new Select(driver.findElement(By
                .cssSelector("select#id_contact")));
        List&lt;WebElement> selectOptions = selectBox.getAllSelectedOptions();
        for (WebElement temp : selectOptions) {
            System.out.println("getText" + temp.getText());
        }
    }

    /**
     * Gets the first selected option in this select tag (or the currently
     * selected option in a normal select)
     */
    @Test
    public void getFirstSelectedOption() {
        Select selectBox = new Select(driver.findElement(By
                .cssSelector("select#id_contact")));
        System.out.println(selectBox.getFirstSelectedOption().getText());
    }

    /**
     * Gets all options belonging to this select tag
     */
    @Test
    public void getOptions() {
        Select selectBox = new Select(driver.findElement(By
                .cssSelector("select#id_contact")));
        List&lt;WebElement> selectOptions = selectBox.getOptions();
        for (WebElement temp : selectOptions) {
            System.out.println("getText" + temp.getText());
        }
    }

    /**
     * Whether this select element support selecting multiple options at the
     * same time? This is done by checking the value of the "multiple"
     * attribute.
     */
    @Test
    public void isMultiple() {
        Select selectBox = new Select(driver.findElement(By
                .cssSelector("select#id_contact")));
        System.out.println(selectBox.isMultiple());
    }

    /**
     * Select all options that have a value matching the argument.
     */
    @Test
    public void selectByValue() {
        Select selectBox = new Select(driver.findElement(By
                .cssSelector("select#id_contact")));
        selectBox.selectByValue("2");
    }

    /**
     * Select the option at the given index.
     */
    @Test
    public void selectByIndex() {
        Select selectBox = new Select(driver.findElement(By
                .cssSelector("select#id_contact")));
        selectBox.selectByIndex(2);
    }

    /**
     * Select all options that display text matching the argument.
     */
    @Test
    public void selectByVisibleText() {
        Select selectBox = new Select(driver.findElement(By
                .cssSelector("select#id_contact")));
        selectBox.selectByValue("Customer service");
    }

}
</code>

9 thoughts on “Control a selectbox or dropdownbox with Selenium WebDriver”

  1. line 158 should read selectBox.selectByVisibleText(“Customer service”);
    Excellent resource by the way

    1. Hello,

      Thanks for your question.

      selectByIndex selects the option based on position in the drop down (indexnumber)
      selectByValue selects the option based on the actual value. (, in this case: selenium)

  2. another way is
    IWebElement dropDownListBox = driver.findElement(By.Id(“selection”));
    SelectElement clickThis = new SelectElement(dropDownListBox);
    clickThis.SelectByText(“Germany”);

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.