How to read from multiples select (drop down winndows) in the same script and store by their labels


I am trying to write a script where i can read all the drop down elements in a web page and store them by categorizing with their parent labels.

Ex: Go the the below website.

You can see “Purpose of Mortgage or Loan” as thee label to identify the first Drop Down in the same way “Type of Property” for the second drop down.

Would like to Write a script in such a way that if i print the options by label, it  should print as

Purpose of Mortgage or Loan
Home Loan
Home Mortgage
Home Mortgage Refinance
Debt Consolidation Loan
Second Mortgage

Type of Property

like this.

Can someone please help me in writing this script and shed some light on the way to achieve it.

Phani M

Default Asked on November 11, 2015 in Selenium WebDriver.
Add Comment
  • 3 Answer(s)

    Lets start with what you have attempted till now? Share the code and we will be able to help you out here.

    Professor Answered on November 13, 2015.
    Add Comment


    Use findElements(), it returns a list of WebElements.

    In your case, you can use findelements on  drop-down elements.Save them in a List<WebElement> of  drop-down elements, iterate over all items in the list and get the require attribute from all elements using get attribute method and print them.

    Give it a try,it’s simple.


    Train Answered on November 13, 2015.
    Add Comment

    Hi Phani,

    Try this , let me know if you require more details.

    WebDriver wd = new FirefoxDriver();



    WebElement MLdropdown = wd.findElement(By.xpath(“.//*[@id=’Field0′]”));
    WebElement TPdropdown = wd.findElement(By.xpath(“.//*[@id=’Field4′]”));

    // ML Dropdown selection

    Select MLselect = new Select(MLdropdown);

    List<WebElement> MLoptions= MLselect.getOptions();

    for (int i=0;i< MLoptions.size();i++){

    String data =MLoptions.get(i).getText();

    // TP Dropdown selection

    Select TPselect = new Select(TPdropdown);

    List<WebElement> TPoptions= TPselect.getOptions();

    //System.out.println(“TP Dropdown Values:”);

    for(int i=0; i< TPoptions.size();i++){

    String data = TPoptions.get(i).getText();


    Default Answered on November 16, 2015.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.