1

I want to select a drop down value. The problem is the drop down list is derived, it is written as list items. Code snippet as follows:

<div class="rcbScroll" style="width: 100%; height: 184px; overflow: auto;">
<ul class="rcbList" style="width: 100%">
 <li class="rcbItem">Option 1</li>
 <li class="rcbItem">Option 2</li>
 <li class="rcbItem">Option 3</li>
</ul>

How do I select 'Option 2' as the selected value in the drop down?

asked Nov 8, 2016 at 3:12
2
  • Is this a ul styled to look like a dropdown? Or did you mean to use an actual select element? Commented Nov 8, 2016 at 5:09
  • This is a ul element styled to look like a drop down. Commented Nov 8, 2016 at 5:31

2 Answers 2

2

using CSS Selectors in Selenium:

you can get value of nth child by finding it with CSS Selector.

// first child: will return "Option 1"
driver.findElement(By.cssSelector("ul.rcbList > li:nth-child(1)"));
// second child: will return "Option 2"
driver.findElement(By.cssSelector("ul.rcbList > li:nth-child(2)"));
// nth child: will return "Option n"
driver.findElement(By.cssSelector("ul.rcbList > li:nth-child(n)"));

In case of when <li> items are dynamic, then get count and loop over and get all the values.

var el_count = driver.FindElements(By.CssSelector("ul.rcbList"));
for(int index=0; index < el_count.count(); index++){
 // 0 (zero) is the first element in <ul> DOM Array
 driver.findElement(By.cssSelector("ul > li:nth-child(index)"));
}

Finding element by Text (value)

Easier way would be finding by xPath and Class

var title = driver.FindElement(By.XPath("./div[@class='aCont']/div/a/span[text() = 'TextToFind']"));
// now title contains text, title = "text to be find"

for more -Example code

answered Nov 8, 2016 at 5:43

5 Comments

Thank you. The first option was sufficient for me.
With a slight change. driver.FindElement(By.CssSelector("ul.rcbList > li:nth-child(1)")).Click(); (where I had to specify the class name of the ulelement)
Is there a syntax I could follow to select the list item by its value? something along the lines that the list item contains "Option 2"
Thank you. Your code seems to be correct. so, I ll mark it as the answer. However, the system I'm working with is a bit more complex to automate. Thanks anyway.
for complex system: 1. get full domain knowledge (not syntax knowledge) 2. python for scrapping 3. R language for scrapping.
0

try (only if ul is treated as dropdown):

SelectElement selectElement = new SelectElement(driver.FindElement(By.cssSelector(".rcbList")));
selectElement.SelectByText("Option 2");

Reference:

https://stackoverflow.com/a/31072586/2575259

answered Nov 8, 2016 at 5:51

1 Comment

Sorry. Didn't work for this scenario. SelectElement can only be used for select-option HTML element, not for unordered list items.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.