Skip to content
Advertisement

How to pass input to multi element text box which is varying dynamically using selenium with java

enter image description hereI have one page where it has single text box, same page we have link called “create account“. When I click “create account“, page will be added with one more text box(i.e now text box count go to 2, next click 3 text box, next click 4 text box, etc). So whenever we click “create account” text box will be added on page.

Now I need to identify how many text boxes available in the column and I need to pass input to those text boxes(i.e if page has 3 text box, I need to identify those 3 text box and send input, if page has 4 text box, I need to identify those 4 text box and send input)

Note: All text boxes will be added after clicking “Create account”

When I use following path, I’m able to identify all available text box( 4 text boxes) in the page Element css path: input.form-url[id^=text-box-]

How I can pass input to these text boxes using selenium java? How to store input using arraylist and how to pass to this css path elements?

Varying text box

String textBoxSelector = "input.form-url[id^=text-box-]";
String[] texts = new String[]{"firstText", "secondText", "thirdText"}; 
for (int i = 0; i < texts.length; i++)                            
{ 
  driver.findElements(By.id(ADDBUTTON)).click(); 
}
List<WebElement> textBoxes = driver.findElements(By.cssSelector(textBoxSelector));
for(int i = 0; i< textBoxes.size(); i++){
    textBox.sendKeys(texts[i]);
}
  • //3 inputs texts in array
  • // first FOR loop clicks button 2 times. so we get total 3 textbixes( 1 default+2 clicks)
  • //list textBoxes store the 3 text box elements
  • //last FOR loop send input values to those 3 text box values

Advertisement

Answer

I assume there is not problems with waiting until all those elements are loaded and if the input text is a fixed value you can do something like this:

String textBoxSelector = "input.form-url[id^=text-box-]";
String[] texts = new String[]{"firstText", "secondText", "thirdText"};
List<WebElement> textBoxes = driver.findElements(By.cssSelector(textBoxSelector));
for(int i = 0; i< textBoxes.size(); i++){
    textBox.sendKeys(texts[i]);
}

UPD
Updated to use existing input strings array you are already have which length is not shorter that the text boxes amount.

Advertisement