0

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'.

3 Answers 3

1

You cannot approach this dropdown with the Select class - it is designed for the select-option regular dropdowns only. Instead, open up the dropdown and select the options manually:

driver.findElement(By.cssSelector('input[value="exampleValue"]')).click();
Sign up to request clarification or add additional context in comments.

2 Comments

How would this work? I can open the dropdown menu no problem, by finding the element and using .click() The problem is that the values are simple <li class="rcbItem">Value</li> elements. I cannot select them properly.
@BobbyMw ah, in that case, locate them as driver.findElement(By.xpath('//li[.="Value"]')).click(). Note that this is all a guessing game for me without being able to actual try that on your particular page. Hope that helps.
0

For this you need to first locate the input tag. This will add all values to List collection. Now traverse through it using for loop and click desired element

WebElement dpdown= driver.findElement(By.xpath("//input[@id='exampleName']"));

List<WebElement> dpListValues=dpdown.findElements(By.tagName("li")); 

 for (int i=0; i<dpListValues.size();i++)
 {
  if ((dpListValues.get(i).getText()).equals("valueYouWantToSelect)"))
    {
        dpListValues.get(i).click();
    }
 }

 }

Comments

0

For selecting those values we can use text() method in the xpath locator. Please find below for the code snippet.

driver.findElement(By.xpath('.//*li[text()="Value"]')).click();

If you want to loop through all the "li" elements, please use below code.

List<WebElement> options = driver.findElements(By.xPath(<locator to find the parent node of all li tags>)).getTagName("li");

for(WebElement eachOpt : options) {
   <Array to store all values> = eachOpt.getText();
   // Logic you want to implement
}

Hope this helps. Please let me know in case you have any issues while using the code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.