0

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.

curiousToKnow
  • 79
  • 1
  • 2
  • 12

4 Answers4

0

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.

meistermeier
  • 7,942
  • 2
  • 36
  • 45
  • @Sham This is the HTML of the form I have issue with. The UI of the page is I have 1 dropdown (with two options as ValueOne and Value Two) and 5 text fields available. By default when I first land on to this page ValueOne is bydefault selected and field 1,2,3 and 5 fields are enabled for user to input data. When I select ValueTwo from the dropdown field 1, 2, 4 and 5 gets enabled. – curiousToKnow Jun 25 '15 at 20:31
  • @Sham The HTML code is exceeding the allowed characters so not sure how to post the complete code. – curiousToKnow Jun 25 '15 at 20:32
  • @ meistermeier Using Firebug I dont see any code as mentioned by you in my page HTML Code. – curiousToKnow Jun 25 '15 at 20:34
  • @reena but this is clearly not a select box but an input field of type text. There isn't any value you could select from the source you provided. I might delete my answer but there are still some comments for sham that would get deleted. – meistermeier Jun 26 '15 at 04:52
  • @ meistermeier I know this is not a select box because I see an exception coming when I do selectByIndex etc. Now the issue is when I do sendKeys still I am not able to pass the value to the dropdown.I am stuck because of this and can progress on my automation from last couple of days. I saw similar problem thread with some other user but at the end of the thread there was no solution provided and I tried all the suggestions provided in the comments but nothing worked for me.Any solution/suggestion is welcome. – curiousToKnow Jun 26 '15 at 14:51
  • Till now I have succeeded in clicking on the dropdown but still not able to select the value. The approach is instead of finding the dropdown element I find the arrow on the dropdown and click it and it shows all the values in the dropdown. I used sendKey but its not working (no exception but value is not selected).One line code: driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Arrow")).click(); – curiousToKnow Jun 26 '15 at 23:39
0

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 :-)

0

You can use this :-

driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input")).sendKeys("ValueTwo", Keys.ARROW_DOWN, Keys.ENTER)
RobC
  • 22,977
  • 20
  • 73
  • 80
0

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();
    }

}