Search code examples
listseleniumxpathdropdown

Error : Getting this exception while trying to select from list dropdown in selenium "option element is not in a select"


I have been trying to select an element from list dropdown(not select dropdown). Everytime I try this, selenium is able to locate the element using xpath but it does not click on the element. Instead it throws an exception that says :

org.openqa.selenium.JavascriptException: javascript error: option element is not in a select

HTML :

<list id="cmds" class="dropdown-menu">

    <option value="{YEAR}">{YEAR}</option>
    <option value="{DAY}">{DAY}</option>
    <option value="{HOME}">{HOME}</option>

</list>

Solution

  • This can be achieved by using the Action library available from selenium.

                import org.openqa.selenium.interactions.Actions;
    
                WebElement newitem= driver.findElement(By.xpath("//option[text()=\"{YEAR}\"]"));
                Actions action = new Actions(driver);
                action.build();
                action.moveToElement(newitem).click();
                action.perform();
    

    This hovers over to the element found by xpath and clicks on it.