Skip to content

How to solve “ Element should have been “select” but was “input”” selenium error

I’m going to get the default selected value of the below dropdown

enter image description here

enter image description here

I wrote below code to do it.

Select selectyear = new Select(driver.findElement("year")));
WebElement year = selectyear.getFirstSelectedOption();
String selectedoption = year.getText();

But this throws the following error Element should have been “select” but was “input”

How can I fix this? same code is working perfectly for dropdowns that don’t have “value” attribute.



The only explanation is there is another element with id year, the input tag.

Put this code before Select selectyear = new Select(driver.findElement("year")));:

List<WebElement> elements = driver.findElements("year"));
        for (WebElement element: elements) {
            System.out.println("Tag: " + element.getTagName());
            System.out.println("Text: " + element.getText());
            System.out.println("Location: " + element.getLocation());

Solution is relative xpath for the select: select[@id='year']