Printing all options from a drop down list selenium

The select dropdown list is a widely used Html web element in web pages. But how to choose its options automatically in selenium automation test scripts? Selenium webdriver provide a class org.openqa.selenium.support.ui.Select to implement this. In this article, we will introduce how to use this class and all its methods step by step.

1. Class org.openqa.selenium.support.ui.Select.

Class org.openqa.selenium.support.ui.Select is used to handle an Html dropdown list web element. Before using it you should import it in your java code. It has the below methods:

1.1 Constructor.

  1. public Select[WebElement element]: This constructor uses an input web element to build a Select object.
  2. The input element must represent an Html SELECT tag, otherwise, an UnexpectedTagNameException will be thrown.

1.2 Get Options Methods.

  1. java.util.List getOptions[]: Return all the dropdown list options web element in a list. You should iterate in the return list to get each option.
  2. java.util.List getAllSelectedOptions[]: Return all the user picked options in a list.
  3. WebElement getFirstSelectedOption[]: Return the first chosen option web element object in the dropdown list.

1.3 Choose Option Methods.

  1. void selectByIndex[int index]: Pick the option in the select list by the given index. The index starts from 0, ends with the Html options index 1.
  2. void selectByValue[java.lang.String value]: Choose all the options that its value attribute match the given value. For example, if the Html select option is as below then we use selectByValue[linux] to choose it.Linux
  3. void selectByVisibleText[java.lang.String text]: Choose the option in the list which option label matching the text argument. For example, if the Html option is as below then useselectByVisibleText[Android] to choose it.Android

1.4 Deselect Option Methods.

  1. void deselectAll[]:Deselect all picked options. Only valid if the html SELECT tags multiple attributes value is true.
  2. void deselectByValue[java.lang.String value]:Deselect all options which value attribute match the argument. For below html optionWindows

    deselectByValue[windows] can be used to deselect it.

  3. void deselectByIndex[int index]: Deselect the option at the given index.
  4. void deselectByVisibleText[java.lang.String text]:Deselect all the optionsthat option label match the input parameter text. For below option, we can usedeselectByVisibleText[MacOs] to deselect it.MacOs

1.5 Check Multiple Method.

  1. boolean isMultiple[]: Check whether this dropdown list allows multiple selections or not. Achieve this by examining the Html SELECT tags multiple attributes value.

2. Example.

This selenium automation test example will use Firefox to open a local web page. There is a dropdown list on the web page. It will do the following actions.

  1. Find the dropdown list web element by its id.
  2. Print all the options labels and values in the dropdown list.
  3. Test all select and deselect methods of Select class.
  4. TestgetFirstSelectedOption[] andgetAllSelectedOptions[] methods.

2.1 Source Code.

  1. TestDropDownInSelenium.htmlPlease choose one or more os that you prefer:

    Windows Linux Unix MacOs IOS Android
  2. com.dev2qa.webdriver.dropdown.TestDropDown@Test /* This method is used to test html SELECT web element operations in selenium webdriver.*/ public void test[] throws InterruptedException { String pageUrl = "file://C:/WorkSpace/dev2qa.com/Code/src/com/dev2qa/webdriver/dropdown/TestDropDownInSelenium.html"; this.ffDriver.get[pageUrl]; /* Find the drop down list web element by it's id. */ By dropDownById = By.id["os"]; WebElement dropDownEle = this.ffDriver.findElement[dropDownById]; if[dropDownEle!=null] { Select dropDownSelect = new Select[dropDownEle]; /* Loop to print all the drop down options label and value.*/ System.out.println["Options for this drop down list."]; this.printOptionsForThisDropDownList[dropDownSelect.getOptions[]]; /* If can choose multiple options. */ if[dropDownSelect.isMultiple[]] { /*Test selectByIndex[],selectByValue[] and selectByVisibleText[] methods. */ dropDownSelect.selectByIndex[1]; Thread.sleep[1000]; dropDownSelect.selectByValue["android"]; Thread.sleep[1000]; dropDownSelect.selectByVisibleText["MacOs"]; }else { dropDownSelect.selectByValue["linux"]; } Thread.sleep[3000]; /* Print out first selected option. */ WebElement firstSelectedOption = dropDownSelect.getFirstSelectedOption[]; this.printDropDownOption[firstSelectedOption, "First selected option"]; Thread.sleep[3000]; /* Loop to print all selected drop down options label and value.*/ System.out.println["Selected Options for this drop down list."]; this.printOptionsForThisDropDownList[dropDownSelect.getAllSelectedOptions[]]; Thread.sleep[3000]; /* If can select multiple options. */ if[dropDownSelect.isMultiple[]] { /*Test Select class's deselectByIndex[], deselectByValue[] and deselectByVisibleText[] methods. */ dropDownSelect.deselectByIndex[1]; System.out.println["Option at index 1 has been deselected."]; Thread.sleep[1000]; dropDownSelect.deselectByValue["android"]; System.out.println["Option has value android has been deselected."]; Thread.sleep[1000]; dropDownSelect.deselectByVisibleText["MacOs"]; System.out.println["Option at label MacOs has been deselected."]; }else { dropDownSelect.deselectByValue["linux"]; } Thread.sleep[3000]; this.selectAll[dropDownSelect]; System.out.println["Select all options."]; Thread.sleep[3000]; dropDownSelect.deselectAll[]; System.out.println["Deselect all options."]; } } /* Select all options in the drop down list. */ private void selectAll[Select dropDownSelect] { if[dropDownSelect!=null] { List optionsList = dropDownSelect.getOptions[]; int size = optionsList.size[]; for[int i=0;i

Chủ Đề