How to send down arrow keys for a drop down?



  • selenium.getDriver().findElement(By.id("s2id_customFilter")).sendKeys(Keys.ARROW_DOWN);
    

    Am using the above code but it's not working."s2id_customFilter" is the id of the drop down.Am clicking on the id and sending keys but there's no action from selenium.

    My code:

    jQueryWebElement filterElement = selenium.jQuery("#s2id_customFilter");
    selenium.jQuery(".select2-choice",filterElement).click();
    JOptionPane.showMessageDialog(null, "one");
    selenium.getDriver().findElement(By.id("s2id_customFilter")).sendKeys(Keys.ARROW‌​_DOWN);
    


  • I really like the answer from the link Philip Beadle posted in a comment:

    It's a bad idea to test the core browser's behaviour as it's not your logic, i.e. you should not need to test that arrow keys work with options since that's something your browser implements.

    And some Protractor (JavaScript) examples how to handle the drop from the same link:

    var options = element(by.id('s2id_customFilter'));
        options.sendKeys('textofyouroption')
    

    And another one when the text changes a lot or is dynamic:

    var optionsElements = element(by.id('s2id_customFilter')).all(by.tagName('option'));
        optionsElements.count().then(function(count) {
          console.log('I have ' + count + ' options')
        });
        optionsElements.get(1).click(); // click on 2nd option
    

    You should be able to do something similar in Java.



Suggested Topics

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