2
IWebElement from = m_driver.FindElement(By.XPath("mydropdownxpath"));
from.Click(); // to open dropdown

My dropdown type is input dropdown and I haven't used SelectElement or IList in my code. How can i get dropdown item count with below as my source code?

<select class="classname" name="ddldropdown" required>

<option data-fas="themered" value="" disabled selected>state</option>

<optgroup label="area">
   <option data-fas="maptheme" value="state1"> state 1 </option>
   <option data-fas="maptheme" value="state2"> state 2 </option>
</optgroup>

<optgroup label="Airport">
   <option data-fas="maptheme" value="ABC"> ABC </option>
   <option data-fas="maptheme" value="XYZ"> XYZ </option>
</optgroup>                       

</select>   

2 Answers 2

4

Your dropdown is <select>, not <input>. SelectElement is the correct way to handle it

IWebElement from = m_driver.FindElement(By.XPath("mydropdownxpath"));
SelectElement select = SelectElement(form);
int count = select.Options.Count; // 5
Sign up to request clarification or add additional context in comments.

6 Comments

SelectElement statement showed - "'Element should have been select but was input'"? Any idea?
@StavanShah It means the element was indeed <input>, but the html you posted doesn't have <input> tag anywhere, it has <select> tag.
the dropdown comes from the mdb purchased theme (mdbootstrap.com/docs/jquery/forms/select)
@StavanShah This is still <select>. Check if the locator it correct.
Guy - do i need to check the Xpath is correct or not? as I am locating my dropdown using relative/absolute xpath
|
0

To make this work, I had to change the second line to

SelectElement select = new SelectElement(form);

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.