2

The website I'm testing has been built with Visual Studio C#. The dropdown menus I'm to test don't use the usual format of:

<select>
    <option>...</option>
    ...
    ...
</select>

Instead, they're built:

<input name="exampleName" type="text" class="rcbInput" id="exampleInput"
  value="exampleValue" readonly="readonly" autocomplete="off">

With each value appearing lower down in the code as a list item.

Each of these list items is within an structure like:

Form > rcbSlide > ... > rcbList > ul > li

Where li objects are the values in the dropdown menu.


What I want to know is, how can I select a dropdown value in this structure?

When I use the standard

Select select = new Select(driver.findElement(By.whatever()))

It gives me an error saying something along the lines of 'Select' expected, got 'input'.

  • Have you checked the answer? – Sagar007 Feb 14 '17 at 10:15
1

One other possibility, I use this when I am desperate, is to simply select the main drop down, then send the down key. The Enter key i.e.

yourOption = element(by.id('exampleInput')).
element(by.xpath("//li[text()='YourText']"));

yourOption.sendKeys(Keys.DOWN);
yourOption.sendKeys(Keys.RETURN);
0

Here Select select = new Select(driver.findElement(By.whatever())) is working only for select HTML tag.

You need to select <input name="exampleName" type="text" class="rcbInput" id="exampleInput" value="exampleValue" readonly="readonly" autocomplete="off">

So follow this steps for solution:

  1. First click on this driver.findElement(By.xpath("//input[@id='exampleInput']")).click();

  2. Then select/click your li element same as above example. driver.findElement(By.xpath("li xpath")).click();

Note : li xpath depends on your HTML code

Let me know If any query regarding this answer.

  • I've tried this. It returns an error, stating that the element is not visible. i.imgur.com/dUIGvIo.png 1 is the dropdown menu element found by ID. 2 is the the value. I got the XPath by using the Chrome Inspector and copying the path. – Bobby Mw Apr 1 '16 at 15:23
  • Please give some time to load page – Sagar007 Apr 1 '16 at 15:32
  • I would highly recommend learning to implement explicit waits effectively. This will allow to wait until the option is displayed and then click it as soon as it is. Link: seleniumhq.org/docs/04_webdriver_advanced.jsp. You'll want to use ExpectedConditions.elementToBeClickable. – VanderLinden Mar 28 '17 at 20:46
0

Most recently I've been working in the Protractor selenium bindings for NodeJS. I'm not sure if this method is universally available for other bindings.

//the first call to element is the group of options in your dropdown
//the second call to element, chained off of the first, looks for subelements in the dom
var yourOption = element(by.id('exampleInput')).element(by.xpath("//li[text()='YourText']"));
yourOption.click()

If something like this is not available in your chosen library you can accomplish the same thing with only xpath

//barebones NodeJS bindings
var yourOption = driver.findElement(By.xpath("//input[@id='exampleInput']//li[text()='YourText']"));
yourOption.click()

The xpath there has an english translation along the lines of "Find an < input > tag with an id of 'exampleInput' then nested at any depth under it in the DOM find an < li > tag which has text inside that matches 'YourText'"

Please note that both of these will only work if the element exists in the DOM at the time the action is performed in your script. If other actions or a wait condition are needed to get the tag to appear you'll need to perform those first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.