'How to get the even numbered options from dropdown in Selenium WebDriver Java

The first 2 options are getting printed instead of 2 and 4.

driver.get("http://output.jsbin.com/osebed/2/");

WebElement fruits = driver.findElement(By.id("fruits"));
Select select = new Select(fruits);
List<WebElement> options = select.getOptions();
int size = options.size();
System.out.println("No of options "+size);
    
for(int i=0;i<options.size();i++) {
    if(i/2==0) {
        //options.
        System.out.println(options.get(i).getText());
    }
}

The first 2 options are getting printed instead of 2 and 4.



Solution 1:[1]

To print the even numbered <option> texts you can increment the counter by 2 as follows:

for(int i=0; i<options.size(); i+2) {
    System.out.println(options.get(i).getText());
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 undetected Selenium