Element should have been "select" but was "div" error



  • 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?


  • QA Engineer

    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^
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2