I would like to only print out items starting with i to s from a dropdown list in amazon. I have the for loop which list them all as in the code below:
driver = new ChromeDriver();
driver.get("https://www.amazon.com");
Actions actions = new Actions(driver);
WebElement ele = driver.findElement(By.xpath("//span[@class='nav-line-2' and contains(.,'Departments')]"));
Thread.sleep(300);
actions.moveToElement(ele);
actions.perform();
WebDriverWait wait = new WebDriverWait(driver, 10);
//List<WebElement> elements = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@id='nav-flyout-shopAll']/div[contains(@class, 'nav-tpl-itemList')]/a")));
List<WebElement> elements = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@id='nav-flyout-shopAll']/div[contains(@class,'nav-tpl-itemList')]//span")));
int itemsCount = elements.size();
System.out.println(itemsCount);
for(WebElement elem: elements) {
System.out.println(elem.getText());
}