Bird
0
0

Identify the error in the following code snippet that tries to select an option by visible text:

medium📝 Debug Q14 of 15
Selenium Java - Handling Form Elements
Identify the error in the following code snippet that tries to select an option by visible text:
WebElement dropdown = driver.findElement(By.name("fruits"));
Select select = new Select(dropdown);
select.selectByVisibleText("Apple");

Assume the dropdown is not a <select> element but a custom div-based dropdown.
ANo error, code works fine
BselectByVisibleText throws NullPointerException
CselectByVisibleText throws NoSuchElementException
DselectByVisibleText throws UnexpectedTagNameException
Step-by-Step Solution
Solution:
  1. Step 1: Check element type requirement for Select

    Select class works only with element causes an error.
  2. Step 2: Identify exception thrown

    When Select is used on a non-, else UnexpectedTagNameException [OK]
Common Mistakes:
  • Assuming it throws NoSuchElementException
  • Ignoring element tag type
  • Expecting NullPointerException

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes