Select functions

A Select function allows you select or deselect values from a drop-down box or a radio button. It includes a list of Selenium API methods to work with select boxes that contain the <select></select> tags. These functions interact with the UI comboboxes to select options.

Select select = new Select(driver.findElement(By.locatorType("path")));

Some helpful snippets using select functions are given below:

  • The selectByIndex(index) method selects an option using the index value. The following is the syntax for this function:
    select.selectByIndex(index);

    Let's get into the bookstore and select a product from a drop-down list. Here, we select the products at the top of the options list using the selectByTndex() method:

    driver.get("http://www.barnesandnoble.com/");
    Select select = new Select(driver.findElement(By.id("quick-search-1-category")));
    select.selectByIndex(1);
    select.selectByIndex(2);
  • The selectByValue(value) method selects an option using value in the string format. The following is the syntax for this function:
    select.selectByValue("value");

    Let's see how we can select an option from the bookstore's category list using the text value:

    driver.get("http://www.barnesandnoble.com/");
    Select select = new Select(driver.findElement(By.id("quick-search-1-category")));
    select.selectByValue("music");
  • The getFirstSelectedOption() method fetches the first selected option or currently selected option from the list.
    select.getFirstSelectedOption();

    Even if there are five options already picked or selected, this method recognizes only the first selected option. In the following code snippet, it returns the currently selected value at option 6:

    driver.get("http://www.barnesandnoble.com/");
    Select select = new Select(driver.findElement(By.id("quick-search-1-category")));
    select.selectByIndex(6);
    WebElement FSO = select.getFirstSelectedOption();
    System.out.println(select.getFirstSelectedOption().getText());
  • The selectByVisibleText(text) method selects an option from the select tag using text visibility. The following is the syntax for this function:
    select.selectByVisibleText("text");

    This method fetches text values, which are highly case sensitive. Let's search for a text value, Music, in the category list:

    driver.get("http://www.barnesandnoble.com/");
    Select select = new Select(driver.findElement(By.id("quick-search-1-category")));
    select.selectByVisibleText("Music");
  • The getAllSelectedOptions() method returns all the selected options. The following is the syntax for this function:
    select.getAllSelectedOptions();

    This method finds all the selected options from the list and returns them. Let's see all the selected categories in this bookstore from the products list in the following snippet of code:

    driver.get("http://www.barnesandnoble.com/");
    Select select = new Select(driver.findElement(By.id("quick-search-1-category")));
    List<WebElement> selectedOptions = select.getAllSelectedOptions();
    for(WebElement b : selectedOptions) {
      System.out.println(b.getText());
    }
  • The getOptions() method returns all data from the options list. The following is the syntax for this function:
    select.getOptions();

    The list values of all the categories are returned from the bookstore, as shown in the following snippet:

    driver.get("http://www.barnesandnoble.com/");
    Select select = new Select(driver.findElement(By.id("quick-search-1-category")));
    for (WebElement b : select.getOptions()) {
      System.out.println(b.getText());
    }
  • The isMultiple() method verifies multiple selection support. The following is the syntax for this function:
    select.isMultiple();

    For instance, the radio buttons or checkboxes have enough reason to have multiple select features; however, some of them might not. This method identifies the multi-select status and quickly responds to the user with valuable feedback. Let's get into a bookstore with a categories (products) combobox that doesn't involve any multi-select features as described:

    driver.get("http://www.barnesandnoble.com/");
    Select select = new Select(driver.findElement(By.id("quick-search-1-category")));
    if(select.isMultiple()){
    System.out.println("Support multiple select at a time");
    }
    else{
    System.out.println("Doesn't support multiple select at a time");
    }
  • The deselectAll() method deselects all the selected options. The following is the syntax of this function:
    select.deselectAll();

    A multi-select box with the options already chosen can obviously be deselected using this select method. Let's see an example in which a web page contains the multi-select box that is chosen with the default option. Finally, the deselectAll() method deselects all the chosen options as follows:

    driver.get("http://compendiumdev.co.uk/selenium/basic_html_form.html");
    Select select = new Select(driver.findElement(By.name("multipleselect[]")));
    select.deselectAll();
  • The deselectByIndex(index) method deselects an option using the index value. The following is the syntax for this function:
    select.deselectByIndex(index);

    This function is the reverse of the selectByIndex(index) method. Let's see how to deselect an option from the multi-select box using an integer value with the following piece of code:

    driver.get("http://compendiumdev.co.uk/selenium/basic_html_form.html");
    Select select = new Select(driver.findElement(By.name("multipleselect[]")));
    select.deselectByIndex(3);
  • The deselectByValue(value) method removes the specific option using value. The following is the syntax of this function:
    select.deselectByValue(value);

    This function is the reverse of the selectByValue(value) method. Let's see how to deselect an option from the multi-select box using a String value with the following piece of code:

    driver.get("http://compendiumdev.co.uk/selenium/basic_html_form.html");
    Select select = new Select(driver.findElement(By.name("multipleselect[]")));
    select.deselectByValue("ms4");
  • The deselectByVisibleText(text) method removes options using text visibility. The following is the syntax of this function:
    select.deselectByVisibleText(text);

    This function is the reverse of the selectByVisibleText(text) method. Let's see how to deselect an option from the multi-select box using String text with the following piece of code:

    driver.get("http://compendiumdev.co.uk/selenium/basic_html_form.html");
    Select select = new Select(driver.findElement(By.name("multipleselect[]")));
    select.deselectByVisibleText("Selection Item 4");
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.96.86