I have an input together with a datalist like
<input type="text" id="theinput" list="thedatalist">
<datalist id="thedatalist">
<option value="one"></option>
<option value="two"></option>
<option value="three"></option>
</datalist>
Now, I want to use selenium to select an item in the datalist.
First, I send a key to the input like
var remoteWebDriver = (RemoteWebDriver) browser;
var input = remoteWebDriver.FindElementsByCssSelector("#theinput");
input.SendKeys("o");
and then I try to treat the datalist as a <select> element like
var datalist = remoteWebDriver.FindElementsByCssSelector("#thedatalist");
var select = new SelectElement(datalist);
I got UnexpectedTagNameException with the message
Element should have been select but was datalist
before I had the chance to select.SelectByText("one");.
How do I interact with the datalist with selenium?
Also, it seems like the dropdown closes when the browser loses focus. Is that expected behavior?
Select the option element from the datalist using the option child position for example, here I am selecting the second option
Then you can get the value of the option by using the getAttribute method
Then you need to input the value into the input field because the datalist provides the values to the input feild: