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



  • 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:

    AL982OK992PO982SX982LQ925
    

    What I want is to be like this:

    AL982
    OK992
    PO982
    SX982
    LQ925
    

    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)



  • \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) {
             input.sendKeys(code+"\n");
       } 
    }
    

    should do what you want.



Suggested Topics

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