I want to iterate through a dropdown, select the first value, then click search, then again select the second value and again perform search.In this code i have tried selecting only one option,but I want to perform search on all the options one by one

  • Try to use Xpath and locate your element – Emna Ayadi Jun 21 '18 at 14:10
  • I used the xpath, but that is not working , it seems to select only one option everytime, i need to iterate through all the options one by one and then click search – ravenclaw_93 Jun 21 '18 at 14:14
  • 1
    Please include the HTML. Since this is not a true HTML <select> tag, it is all simulated and can be designed in unlimited ways and thus is very unlikely to really help you. – MivaScott Jun 21 '18 at 22:22

Since you say this not a true HTML tag, it is all simulated and can be designed in unlimited ways. Until the actual HTML is actually included the best we can offer is generic advice...

Step 1: Identify the link to expand the select options

WebElement selectLink = driver.findElement(By.id(buttonId));

Step 2: Identify the individual links for each of the options in the simulated select

List<WebElements> optionLinks = driver.findElements(By.cssSelector(commonOptionPath));

Step 3: Write a method to click the select link (to "pop open" the select options)

public void clickSelectLink() {

Step 4a: Write a method to search through the options list comparing text with what you want to click

public void clickOptionByName(String name) {
    for (int i = 0; i < optionLinks.size(); i++) {
        if (optionLinks().get(i).getText().equalsIgnoreCase(name)) {
    // Error out because we could no find a match

Step 4b: Write a method that clicks on a specific index in the option list

public void clickOptionByIndex(int index) {

Unfortunately, there are very few shortcuts possible. Since it is all simulated, there are no built-in selenium methods to handle this. The only possibility is that the developer inserted a non-random ID for each of the options, but this would be the .01% chance.

  • The loop can be more simply written with a filter: optionLinks.filter(link->name.equals(link.getText()) .collect(Collectors.toList()).get(0).click(); – João Farias Jun 22 '18 at 0:15
  • My requirement is actually to click on the option in the dropdown one by one and then click search i.e. I will first select the first option then search , then again go back to the second option and click search. This is to be continued till the last option – ravenclaw_93 Jun 22 '18 at 14:55
  • @shashi, in which case for the actual test you would create a for loop of optionLinks.size(). In this loop you click the link to open the options, click on the next index link, and perform any other evaluations you need. – MivaScott Jun 22 '18 at 15:46
  • @mivascott-with reference to my code snippet shared can you help me out with this please – ravenclaw_93 Jun 23 '18 at 1:37

Use findElements and get the list of all the elements matching under the class.

Then use foreach loop to traverse through the loop and verify if the text contains in the list of web element then click on it.

List<WebElement> options = driver.findElementsByTagName("tagname");

for(WebElement sample : options)


if(sample.getText().equals("text in your application"))






Once selected click on the search using the locator.

  • My code has div tag for most of the elements so searching the required text is taking too much time, anything else than tagname would be helpful – ravenclaw_93 Jun 21 '18 at 15:21
  • Then select a particular div tag using the class or id of the div and click the value. – Prasanna venkatesh Jun 21 '18 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.