Keyboard ArrowDown in listbox using Selenium with C#



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


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


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2