21

I am using Selenium Java. I need to enter value into text box and press down arrow to select suggestions and then press Enter key.

So, my question is how to press Down Arrow key followed by "Enter" key?

2

6 Answers 6

41

You can import Keys and use these.

import org.openqa.selenium.Keys

WebElement.sendKeys(Keys.DOWN);
WebElement.sendKeys(Keys.RETURN);

Edit

You could probably use one sendKeys() call:

WebElement.sendKeys(Keys.DOWN, Keys.RETURN);
1
  • For me just worked with Keys.ARROW_DOWN and not Keys.DOWN Jun 2, 2020 at 14:56
2

For Ruby, this would be:

input_element = @driver.find_element(:id,'input_id')
input_element.send_keys(:arrow_down)

A list of special character keys can be found here

0
using Keys = OpenQA.Selenium.Keys;

//moves down arrow key from keyboard to the list of dropdown
IWebElement.SendKeys(Keys.Down);
//Hits Enter on the selected list from the dropdown
IWebElement.SendKeys(Keys.Return);

This will work.

1
  • Please read How to Answer and edit your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post.
    – Adriaan
    Oct 20, 2022 at 14:41
0
driver.findelement(By.(locator(locator details)).sendKeys(Keys.ARROW-DOWN,Keys.RETURN)
1
  • Please read How to Answer and edit your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post.
    – Adriaan
    Oct 20, 2022 at 14:41
0

I have tried this and it worked for me.

WebElement dp_down = driver.findElement(By.xpath("enter-your-element-xpath-here");
dp_down.sendKeys(Keys.ARROW_DOWN, Keys.RETURN);

This is working fine for me without any issues. CHEERS!!!

-1

Even you can concatenate both the Down and Enter in a single statement.

import org.openqa.selenium.Keys
WebElement.sendKeys(Keys.DOWN + Keys.ENTER);
1
  • OP asked for sequential, not pressing at the same time
    – Clay H
    Dec 20, 2017 at 14:04

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.