0

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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 2
    Does this answer your question? [What's the syntax for mod in java](https://stackoverflow.com/questions/90238/whats-the-syntax-for-mod-in-java) – seenukarthi Mar 28 '22 at 09:53

1 Answers1

0

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());
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352