1

i am new to Java and Selenium and try to learn The Problem is, that i do not know how to extract names from a Dropdown Menu using the Select class

I have created a Variable Array String with the Expected Results
I have created a new object for Select Class and pass the xpath
I have created a List to store all the options from the Dropdown
Then i have a For Loop to iterate in all items in the loop and take the names
But i have no method to extract the name.
The question is how we can do that? Using the Select.

Thanks in advance for your help

Practice Website is this one: http://qaclickacademy.com/practice.php

HTML CODE:

<select id="dropdown-class-example" name="dropdown-class-example" wtx-context="4CBF2E98-EC2A-4384-8B92-4FB100C9F504" style="" xpath="1">
    <option value="">Select</option>
    <option value="option1" style="">Option1</option>
    <option value="option2">Option2</option>
    <option value="option3">Option3</option>
</select>

MY CODE:

String expectedDropDownItems [] = {"Select","option1","option2","option3"};
Select dropDownSelect = new Select(driver.findElement(By.id("dropdown-class-example")));
List<WebElement> dropDownActualValues = dropDownSelect.getOptions();
int counter = dropDownActualValues.size();
for (int i=0; i < counter; i++){

}

Expected and Actual something like this :

Selection 1 is : Select  
Selection 2 is : option1  
Selection 3 is : option2  
Selection 4 is : option3  
1

1 Answer 1

2

you have all options in list of dropdown element from here you are very close to what you want. There is method on every web element from WebElement Interface as getText()

In your for loop it will look like as :

for (int i=0; i<counter; i++){
            System.out.println("Selection " + i + " is : "  + dropDownActualValues.get(i).getText());
   }

output
Selection 0 is : Select
Selection 1 is : Option1
Selection 2 is : Option2
Selection 3 is : Option3

Sign up to request clarification or add additional context in comments.

1 Comment

Thank a lot, yes i was trying to find getText, but i had to return the (i). Got it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.