Friday, 8 July 2016

How to get list of elements from drop down

How to get list of elements from drop down:
==================================


package com.rameshsoft.draganddrops;;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class TotalElementsWebList {
public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.facebook.com");
WebElement element_day = driver.findElement(By.id("day"));
Select select = new Select(element_day);
// getting all elements from droip down
List<WebElement> totalWebListElements = select.getOptions();
// removing duplicate elements from list if any
HashSet<WebElement> hashSet = new HashSet<WebElement>(totalWebListElements);
// iterating the elements from collection object
Iterator<WebElement> iterator = totalWebListElements.iterator();
while (iterator.hasNext()) {
WebElement webElement = (WebElement) iterator.next();
if (webElement.getText().equalsIgnoreCase("15")) {
webElement.click();

}
}

}
}

No comments:

Post a Comment