1

I get the error message when I try to select an item from a menu in a webpage. In the code below, I used the Id of the element. When I tried using the XPath instead of the Id, I got a similar error messsage.

Here is the error in full:

An unhandled exception of type 'OpenQA.Selenium.Support.UI.UnexpectedTagNameException' occurred in WebDriver.Support.dll

Additional information: Element should have been select but was div

And, here is the code:

        //Select "Perday" for Salary Type   
        var salType = driver.FindElement(By.Id("adv-search-mobile"));
        var selectSalType = new SelectElement(salType);

        selectSalType.SelectByText("Per day");

    }
}

HTML:

<select class="adv-search-select sal-type-select" name="salarytype" tabindex="6"> 
    <option value="annum">Per annum</option> 
    <option value="month">Per month</option> 
    <option value="week">Per week</option> 
    <option value="day">Per day</option> 
    <option value="hour">Per hour</option> 
 </select> 
alecxe
  • 11,425
  • 10
  • 49
  • 107
OA345
  • 545
  • 1
  • 10
  • 19

2 Answers2

4

The error message is just what it says. The element on your page with ID adv-search-mobile has a <div> tag.

You need to update your locator to get the <select> element (within that <div>. A SelectElement object can only be created from a <select>.

If you need more help, you can post your HTML.

EDIT: If the name of your dropdown is unique, you might try:

var salType = driver.FindElement(By.Name("salarytype"));
FDM
  • 5,894
  • 1
  • 16
  • 34
  • Is this what you mean, please:

    select class="adv-search-select sal-type-select" name="salarytype" tabindex="6">

    – OA345 Jun 29 '17 at 07:08
  • See my edit. If that doesn't work, post the
    tag above the
    – FDM Jun 29 '17 at 07:13
0
<a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">

        Live Actions<b class="caret"></b></a>

enter image description here

Kate Paulk
  • 31,513
  • 8
  • 54
  • 108
essam
  • 1