1

I'm working in selenium c# automation, I'm trying to traverse down in list box values. <ul> and <li>. I have tried many options in trial and error. The kind of code I'm trying to use is below. please find my piece of code.

I don't know why this does not work.

var resultset = _driver.FindElement(By.XPath("//*[@id='Narrative_listbox']"));
var options = resultset.FindElements(By.TagName("li"));

foreach (var option in options)
{
   option.SendKeys(Keys.ArrowDown);
}
  • Try to send arrowdown key to the resultset, not to its options – wec Nov 14 at 18:21
  • What happens when you try to run your code? – Kate Paulk Nov 14 at 20:00
  • @wec I can't get you, what you are trying to say kindly elaborate it – Thiyagarajan Nov 15 at 7:33
  • @KatePaulk Getting error : element not interactable – Thiyagarajan Nov 15 at 7:38
  • 1
    @Thiyagarajan I mean what if instead option.SendKeys(Keys.ArrowDown); you would use resultset.SendKeys(Keys.ArrowDown);? – wec Nov 15 at 10:02
0

If you make sure the right element has the focus you could use Actions to send keyboard commands to the browser.

Actions actions = new Actions(driver);
actions.SendKeys(Keys.ArrowDown).Build().Perform();

In your situation this could look like:

var resultset = _driver.FindElement(By.XPath("//*[@id='Narrative_listbox']"));
resultset.Click(); // Give it focus
var options = resultset.FindElements(By.TagName("li"));

foreach (var option in options)
{
    Actions actions = new Actions(driver);
    actions.SendKeys(Keys.ArrowDown).Build().Perform();
}
  • Yes,It has selected the first item not travelling through rest of items. – Thiyagarajan Nov 15 at 14:33
  • Not sure what you are saying, but I added an example for your situation. – Niels van Reijmersdal Nov 15 at 15:17
  • van Rejimersdal sorry for late reply its working – Thiyagarajan Nov 18 at 6:22
  • @Rejimersdak is it possible to get the count from resultset or get the text or iteration count – Thiyagarajan Nov 19 at 14:03
  • @Thiyagarajan Yes, FindElements returns an ReadOnlyCollection<IWebElement> which has a Method Count. See the documentation: selenium.dev/selenium/docs/api/dotnet/html/… – Niels van Reijmersdal Nov 19 at 14:15

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.