I have a text field to input voucher code, and I already tried this;

driver.findElement(By.id("voucher_code")).sendKeys("AL982", "OK992", "PO982", "SX982", "LQ925");

In the text field it become like this:


What I want is to be like this:


Because the system read, 1 row = 1 voucher code.

  1. How do I use sendKeys to input multiple value in different rows?

  2. And is it possible to shorten my code for multiple random value like above?, (in real case we have 1000 voucher code)

  • Manual test for automation above will be like this; type voucher code 1 -> press keyboard Enter -> type voucher code 2-> and so on.
    – BetaTester
    Nov 6, 2018 at 8:11

2 Answers 2


\n is your friend here. sendKeys actually recognizes them as linebreaks, at least in chrome, ff an edge. Can't say anything about other webdriver implementations as I don't use them.

void addVouchers(String[] codes, WebElement input) {
    for(String code: codes) {

should do what you want.

  • /n is my new friend
    – BetaTester
    Nov 6, 2018 at 9:32
  • Happy to help and thanks for the check mark.
    – Daniel
    Nov 6, 2018 at 9:41

When you are passing comma seperated value, it will append all of it and pass it to field.

you can try this to pass multiple values in different fields.

String[] voucherCodes = {"AL982", "OK992", "PO982", "SX982", "LQ925"};
WebElement field = driver.findElement(By.id("yourIdforEveryField"));
for (String voucher : voucherCodes) {
  • Tried and It didn't work, all value still appear in the same row within text field.
    – BetaTester
    Nov 6, 2018 at 7:48
  • Try this - String[] voucherCodes = {"AL982", "OK992", "PO982", "SX982", "LQ925"}; String[] fieldIds = {"abc1", "abc2", "abc3", "abc4", "abc5"}; for (int i = 0; i < fieldIds.length; i++) { WebElement field = driver.findElement(By.id(fieldIds[i])); field.sendKeys(voucherCodes[i]); }
    – Joshi
    Nov 9, 2018 at 4:56

Your Answer

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

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