What is the output of this Selenium Java code snippet?
Consider the following Selenium Java code that selects an option from a dropdown by visible text. What will be the output if the dropdown contains options: "Red", "Green", "Blue" and the code selects "Green"?
The method selectByVisibleText chooses the option matching the exact visible text.
✗ Incorrect
The Select class's selectByVisibleText method selects the option with the exact visible text. Then getFirstSelectedOption returns that option's text, which is "Green" here.
❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies the selected dropdown option?
You want to verify that the dropdown with id "fruits" has "Apple" selected after selection. Which assertion is correct in Java with TestNG?
Selenium Java
WebElement dropdown = driver.findElement(By.id("fruits"));
Select select = new Select(dropdown);
select.selectByVisibleText("Apple");
Check the selected option's visible text, not the dropdown element's text or options list.
✗ Incorrect
getFirstSelectedOption().getText() returns the visible text of the selected option. This is the correct way to assert the selected value.
❓ locator
advanced
2:00remaining
Which locator is best to find a dropdown element for Select class usage?
You want to locate a dropdown for selecting a country. The dropdown has a unique id "country-select". Which locator is best practice for Selenium?
ABy.cssSelector("select[name='country']")
BBy.id("country-select")
CBy.xpath("//select[@name='country']")
DBy.className("dropdown")
Attempts:
2 left
💡 Hint
Using unique ids is the fastest and most reliable locator.
✗ Incorrect
By.id("country-select") is the best locator because ids are unique and fast to locate elements.
🔧 Debug
advanced
2:00remaining
Why does this code throw UnexpectedTagNameException?
Analyze the code below. The dropdown element is a
with options inside, not a
Selenium Java
WebElement dropdown = driver.findElement(By.id("custom-dropdown"));
Select select = new Select(dropdown);
select.selectByIndex(1);
ASelect class only works with <select> HTML elements, not <div> or custom dropdowns.
BThe dropdown id is incorrect, so element is not found.
CselectByIndex requires zero-based index, 1 is invalid here.
DThe driver is not initialized properly before usage.
Attempts:
2 left
💡 Hint
Select class expects a
✗ Incorrect
Select class constructor checks if the element is a
❓ framework
expert
3:00remaining
How to design a reusable method to select dropdown options by visible text?
You want to create a reusable Java method in your Selenium framework to select any dropdown option by visible text. Which method signature and implementation is best?
A
public void selectDropdownOption(WebDriver driver, By locator, String visibleText) {
WebElement dropdown = driver.findElement(locator);
Select select = new Select(dropdown);
select.selectByVisibleText(visibleText);
}
B
public void selectDropdownOption(By locator, String visibleText) {
Select select = new Select(driver.findElement(locator));
select.selectByVisibleText(visibleText);
}
C
public void selectDropdownOption(String id, String visibleText) {
WebElement dropdown = driver.findElement(By.id(id));
Select select = new Select(dropdown);
select.selectByVisibleText(visibleText);
}
D
public void selectDropdownOption(WebElement dropdown, String visibleText) {
Select select = new Select(dropdown);
select.selectByVisibleText(visibleText);
}
Attempts:
2 left
💡 Hint
Passing WebElement is more flexible and avoids driver dependency inside method.
✗ Incorrect
Option D accepts a WebElement dropdown and visible text, making it reusable and decoupled from driver or locator details.
Practice
(1/5)
1. What is the main purpose of the Select class in Selenium WebDriver?
easy
A. To perform mouse hover actions
B. To locate elements on the web page
C. To handle dropdown menus created with the <select> HTML tag
D. To take screenshots of the web page
Solution
Step 1: Understand the Select class role
The Select class is designed specifically to interact with dropdown menus that use the <select> HTML element.
Step 2: Compare with other Selenium features
Locating elements, mouse actions, and screenshots are handled by other classes and methods, not Select.
Final Answer:
To handle dropdown menus created with the <select> HTML tag -> Option C
Quick Check:
Select class = dropdown handler [OK]
Hint: Select class works only with <select> dropdowns [OK]
Common Mistakes:
Using Select for non-<select> dropdowns
Confusing Select with Actions class
Thinking Select locates elements
2. Which of the following is the correct way to create a Select object for a dropdown WebElement named dropdown?
easy
A. Select select = new SelectElement(dropdown);
B. Select select = new Select(dropdown);
C. Select select = Select(dropdown);
D. Select select = new select(dropdown);
Solution
Step 1: Recall Select class constructor syntax
The Select class constructor requires the WebElement as an argument and is called with 'new Select(element)'.
Step 2: Check Java syntax correctness
Java is case-sensitive; class names start with uppercase. The correct syntax is 'new Select(dropdown);'. Other options have syntax errors or wrong class names.
Final Answer:
Select select = new Select(dropdown); -> Option B
Quick Check:
Use 'new Select(element)' to create Select object [OK]
Hint: Use 'new Select(element)' with uppercase S [OK]
Common Mistakes:
Missing 'new' keyword
Wrong class name like SelectElement
Incorrect capitalization
3. Given the following code snippet, what will be the selected option's visible text after execution?
Hint: Select only works on <select>, else UnexpectedTagNameException [OK]
Common Mistakes:
Assuming it throws NoSuchElementException
Ignoring element tag type
Expecting NullPointerException
5. You have a dropdown with options: "Small", "Medium", "Large", "Extra Large" Write the best way to select "Extra Large" using the Select class if you only know the option's value attribute is "xl" but the visible text is dynamic. Which method should you use?
hard
A. select.selectByValue("xl");
B. select.selectByVisibleText("Extra Large");
C. select.selectByIndex(3);
D. select.selectByPartialVisibleText("Large");
Solution
Step 1: Understand selection methods
selectByVisibleText requires exact visible text, which is dynamic here, so not reliable.
Step 2: Use value attribute for stable selection
selectByValue("xl") uses the option's value attribute, which is stable and known.
Step 3: Evaluate other options
selectByIndex(3) depends on position which may change; selectByPartialVisibleText does not exist in Select class.
Final Answer:
select.selectByValue("xl"); -> Option A
Quick Check:
Use selectByValue for stable attribute selection [OK]
Hint: Use selectByValue when visible text is unreliable [OK]
Common Mistakes:
Using selectByVisibleText with dynamic text
Using invalid method selectByPartialVisibleText
Relying on index which may change
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.