0
0
Selenium Javatesting~20 mins

Select by value, visible text, index in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dropdown Selection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the selected option's visible text after selecting by value?
Given a dropdown with options:
  • <option value='1'>Apple</option>
  • <option value='2'>Banana</option>
  • <option value='3'>Cherry</option>

What will be the visible text of the selected option after running this code?
WebElement dropdown = driver.findElement(By.id("fruits"));
Select select = new Select(dropdown);
select.selectByValue("2");
String selectedText = select.getFirstSelectedOption().getText();
Selenium Java
WebElement dropdown = driver.findElement(By.id("fruits"));
Select select = new Select(dropdown);
select.selectByValue("2");
String selectedText = select.getFirstSelectedOption().getText();
ABanana
BCherry
C2
DApple
Attempts:
2 left
💡 Hint
Selecting by value picks the option whose value attribute matches the given string.
query_result
intermediate
2:00remaining
What is the selected option's value after selecting by visible text?
Given a dropdown with options:
  • <option value='a'>Red</option>
  • <option value='b'>Green</option>
  • <option value='c'>Blue</option>

What will be the value attribute of the selected option after running this code?
WebElement dropdown = driver.findElement(By.id("colors"));
Select select = new Select(dropdown);
select.selectByVisibleText("Blue");
String selectedValue = select.getFirstSelectedOption().getAttribute("value");
Selenium Java
WebElement dropdown = driver.findElement(By.id("colors"));
Select select = new Select(dropdown);
select.selectByVisibleText("Blue");
String selectedValue = select.getFirstSelectedOption().getAttribute("value");
Ac
Bb
Ca
DBlue
Attempts:
2 left
💡 Hint
Selecting by visible text picks the option whose text matches exactly.
query_result
advanced
2:00remaining
What is the visible text of the selected option after selecting by index?
Given a dropdown with options:
  • <option value='x'>Small</option>
  • <option value='y'>Medium</option>
  • <option value='z'>Large</option>

What will be the visible text of the selected option after running this code?
WebElement dropdown = driver.findElement(By.id("sizes"));
Select select = new Select(dropdown);
select.selectByIndex(0);
String selectedText = select.getFirstSelectedOption().getText();
Selenium Java
WebElement dropdown = driver.findElement(By.id("sizes"));
Select select = new Select(dropdown);
select.selectByIndex(0);
String selectedText = select.getFirstSelectedOption().getText();
ALarge
BMedium
CSmall
D0
Attempts:
2 left
💡 Hint
Index starts at 0 for the first option in the dropdown.
📝 Syntax
advanced
2:00remaining
Which option causes a compile-time error when selecting by visible text?
Which of the following code snippets will cause a compile-time error in Java when trying to select an option by visible text from a dropdown?
Aselect.selectByVisibleText("Option 1");
Bselect.selectByVisibleText("Option1");
Cselect.selectByVisibleText("option1");
Dselect.selectByVisibleText(Option1);
Attempts:
2 left
💡 Hint
Check if the argument is a valid string literal.
🧠 Conceptual
expert
2:00remaining
Why might selectByIndex be less reliable than selectByValue or selectByVisibleText?
Consider a dropdown menu whose options might change order dynamically. Which reason best explains why using selectByIndex to select an option can cause problems compared to selectByValue or selectByVisibleText?
ABecause selectByIndex is slower than selectByValue or selectByVisibleText.
BBecause selectByIndex depends on the position of options, which can change, causing wrong selections.
CBecause selectByIndex only works with numeric visible text, which is rare.
DBecause selectByIndex requires the value attribute to be unique, which is not always true.
Attempts:
2 left
💡 Hint
Think about what changes in the dropdown might affect the index.