javaseleniumwebdriverkeydowntypeahead

Getting Java Webdriver 'Down Arrow' action to work


So I have been trying to resolve this for several hours. I have no clue what I am doing wrong.

This is a type ahead field I am looking in is <input type="text" id="id_attendees" name="attendees">. When I type in there a js dropdown is created. When I press the Down Arrow on keyboard it works fine and selects the top choice. When I do keyDown --- id=id_attendees --- \40 in IDE it works fine and also selects the choice.

I cannot get it to do the same in Java webdriver though

Actions actionObject = new Actions(driver);
actionObject.sendKeys(Keys.ARROW_DOWN);

^doesn't work.

driver.findElement(By.id("id_attendees")).sendKeys(Keys.ARROW_DOWN);

^doesn't work

I tried Keys.DOWN in both cases, that doesn't work either. I created a literal String altm = "\u0040"; and all that does is type an @ symbol.

I also tried a bunch of other things as well and nothing is working. I have no clue what am I missing.

EDIT 1:

@Roddy Thank you! - Given that link I added the following that did work (after importing DefaultSelenium and WebDriverBackedSelenium.

DefaultSelenium sel = new WebDriverBackedSelenium(driver,vars.siteurl);
sel.fireEvent("//input[@id='id_attendees']", "keydown");

EDIT 2: --> DOH that doesn't work. I got overzealous apparently.


Solution

  • With Actions class, after defining what it will do for you, you need to first build() it. So in your case it would be like this:

    Actions actionObject = new Actions(driver);
    actionObject.sendKeys(Keys.ARROW_DOWN).build();
    

    When you want your script to execute that action, you need to perform() it. You can chain it right after your build() method (if you are using it just once, for example) or later in your code whenever you need it, like this:

    actionObject.sendKeys(Keys.ARROW_DOWN).build().perform();
    

    OR

    actionObject.perform();
    

    Good luck!