0

I have the following WebElement List in a page

@FindBy(xpath="a")
private List<WebElement> generalList ;
@FindBy(xpath="b")
private List<WebElement> eventList ;
@FindBy(xpath="c")
private List<WebElement> additionalList ;

I want to iterate each of the above list items and input values through a loop

Example

Generalist

Last Name: last name (input field)

First Name: First Name (input field)

AdditionalList

Address : Adress (input field)

Zip :11423 (input field)

I have tried the following

generalList.stream().forEach(elem -> elem.sendkeys("last name"," First Name"));

Its entering lastnameFirstname in each input field.

Last Name: lastnameFirstname

First Name: lastnameFirstname

2 Answers 2

1

Your code already iterates the list and enters data in each element.

Honestly, this is not a good way to do this. I'm guessing you might think this is more efficient or whatever but it just makes it more confusing. Just .sendKeys() to Last Name, then First Name, and so on. Doing it this way makes it more clear what you are trying to do.

Sign up to request clarification or add additional context in comments.

Comments

0

Actually you are looping through WebElement and provide same value during sendKeys() that's why you are getting same output which is absolutely correct. You need to loop through value which you want to set on input field instead as below :-

String[] names = {"Last Name", "First Name"};
IntStream.range(0, names.length).forEach(idx -> generalList.get(idx).sendKeys(names[idx]));

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.