Bird
0
0

Given the following code:

medium📝 Predict Output Q5 of 15
Selenium Java - Handling Form Elements
Given the following code:
WebElement dropdown = driver.findElement(By.name("fruits"));
Select select = new Select(dropdown);
select.selectByValue("banana");
System.out.println(select.getFirstSelectedOption().getAttribute("value"));

What will be printed if the dropdown options have values: apple, banana, cherry?
Acherry
Bbanana
Capple
Dnull
Step-by-Step Solution
Solution:
  1. Step 1: Understand selectByValue()

    The method selectByValue("banana") selects the option whose value attribute is "banana".
  2. Step 2: Print selected option's value attribute

    The printed attribute is "banana" as it matches the selected option.
  3. Final Answer:

    banana -> Option B
  4. Quick Check:

    selectByValue("banana") prints "banana" [OK]
Quick Trick: selectByValue() matches option's value attribute [OK]
Common Mistakes:
  • Printing visible text instead of value
  • Using wrong attribute name
  • Assuming selectByValue selects by visible text

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes