Exception in thread : UnexpectedTagNameException

2019-09-11 06:15发布

问题:

I am Trying to Locate Dropdown using Select but its giving me error :

Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "input"

Tried with ByIndex,ByValue but its not working

Code

Select dropdown = new 
Select(driver.findElement(By.id("ctl00_MainContent_ddlLocale_Input")));
    //dropdown.selectByIndex(2);
     dropdown.selectByValue("Austria: Vienna");

HTML

td class="rcbInputCell rcbInputCellLeft" style="width:100%;">
<input id="ctl00_MainContent_ddlLocale_Input" class="rcbInput ui-widget-    content" type="text" value="Austria: Vienna"     name="ctl00$MainContent$ddlLocale" autocomplete="off"/>

回答1:

As exception clearly saying you are locating the input element but trying to work as select element.

new Select() expect select element as input while you are providing input element as input.

You need to verify your provided id ctl00_MainContent_ddlLocale_Input is the id of input element or select element.

If in your case the ctl00_MainContent_ddlLocale_Input same for both input and select elements, then you need to try usimg cssSelector to specify select element as below :-

Select dropdown = new Select(driver.findElement(By.cssSelector("select#ctl00_MainContent_ddlLocale_Input"))); 

//dropdown.selectByIndex(2);
dropdown.selectByValue("Austria: Vienna");

Hope it helps..:)