'How to select a value from input type dropdown using selenium webdriver?

I am trying to select a value available in a read only drop down and I have tried so many options but still failing to select the desired option. The drop down has two values available ValueOne and ValueTwo. By default the ValueOne is selected and in my case I need to select ValueTwo. I used firebug to get the below code when I click on the drop down and do Inspect Element with firebug The Code is :

<td class="rcbInputCell rcbInputCellLeft" style="width:100%;">
<input id="ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input" class="rcbInput radPreventDecorate" type="text" readonly="readonly" value="ValueOne" name="ctl00$ContentPlaceHolder1$RadGrid1$ctl00$ctl02$ctl02$EditFormControl$rcbControllerType1" autocomplete="off">
</td>

So far I have tried

1----------

Select DropDown = new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input")));
        DropDown.selectByVisibleText("ValueTwo");

and I get an exception as

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

2------------

WebElement Dropdown = driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input"));
        Select clickThis = new Select (Dropdown);
        clickThis.selectByVisibleText("ValueTwo");

Get Exception:

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

I also tried selectByIndex but still get the above exception message.

3--------------

driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input")).sendKeys("ValueTwo");

Nothing happens and the case is marked as Pass. No error no exception.

Also I am running my webscript on firefox 38.0.5 with selenium 2.46.0 with eclipse TestNG. I have confirmed the frame is not an iframe.

Please suggest the solution.



Solution 1:[1]

The root problem might be, that this is not a standard select box but an javascript based solution. The code above just shows the 'host' html element. I am pretty sure that there will be a) a previous hidden element that becomes visible or b) a newly created element in your DOM that holds the values. You have to find those (dev tools or firebug) to interact with. Some pseudo code that might appear (just to get a hint):

<ul>
  <li id="element1">ValueOne</li>
  <li id="element2">ValueTwo</li>
</ul>

And after it appears (wait for in selenium) you just have to click the desired element.

Solution 2:[2]

Find your xpath through firepath addon in firefox.

driver.findElement(By.xpath(".//*@id='ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input']")).click();

Select value in dropdown ->goto firpath by right click and copy xpath

driver.findElement(By.xpath(".//*@id='ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input']/span[3]")).click();

hope you will find your solution :-)

Solution 3:[3]

You can use this :-

driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input")).sendKeys("ValueTwo", Keys.ARROW_DOWN, Keys.ENTER)

Solution 4:[4]

you can use the following code. Here what I have done is find the dropdown, click on it and find the option to select and send down key until we see the element to select and after click on it.

public class InputDropdownselect {
@Test
public void login() throws Exception
{
System.setProperty("webdriver.chrome.driver", "G:\\drivers\\chrome\\chromedriver_win32\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://yourwebsite.com");
driver.manage().window().maximize();
driver.findElement(By.id("txtuser")).sendKeys("123456");
driver.findElement(By.id("txtpassword")).sendKeys("Abc!@1");
driver.findElement(By.id("log-btn")).click();
Thread.sleep(2000);
driver.findElement(By.id("enrollment")).click();
//driver.findElement(By.xpath("//*[@id=\"enrollment\"]")).click();
driver.findElement(By.xpath("//*[@id=\"studentBasicForm\"]/div[2]/div[9]/div/div/input")).click();
Actions action= new Actions(driver);
WebElement joiningYear=driver.findElement(By.xpath("//input[@placeholder=\"Joining Year Group\"]/following::ul[1]/descendant::li/following::span[contains(text(),\"8\")]"));
do {
    action.sendKeys(Keys.ARROW_DOWN).perform();
} while (!joiningYear.isDisplayed());
joiningYear.click();
    }

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 meistermeier
Solution 2 sukriti
Solution 3 RobC
Solution 4 user2439599