0

HTML code of the desired element:

<select class="fcr-height-parent oj-select-select oj-component-initnode oj-select-options-generated" data-bind="attr:{id: idTag, name: idTag}, id="40026501instrChqClgType" name="40026501instrChqClgType" aria-required="true" style="border-color: rgb(4, 95, 171);"><option class="oj-listbox-result oj-listbox-result-selectable oj-listbox-results-depth-0" role="option" id="oj-listbox-result-label-14" value=""></option></select></div>

Selenium code:

new Select(driver.findElement(By.xpath(//*[contains(@id,'instrChqClgType')])))

Getting the following error:

org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "div"

What could be the reason?

4

The problem is that your XPath expression matches a div element while Select class constructor expects a select element.

This can be explained by the use of a wildcard * in your XPath expression. Or, to put it differently, your selector matches another element, which happens to be a div element, which has an id containing instrChqClgType.

A quick and easy way to fix it, would be to explicitly search for select instead of *:

new Select(driver.findElement(By.xpath("//select[contains(@id,'instrChqClgType')]")))
//                                        ^HERE^
1

Selenium Select class is works for HTML <select> tag. refer this

In your code, you have written the xpath which is locating any element which contains id = instrChqClgType. That's the reason it locating the div tag with the above mentioned id instead of <select> tag and throwing the error.

Solution: refer @alecxe solution using xpath :

//select[contains(@id,'instrChqClgType')]

OR try some alternate locator like CSS selector

select[name$='instrChqClgType']

This will locate the select tag having said text in the ending of name attribute. and below for id attribute

select[id$='instrChqClgType']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.