Cloudravi
Cloudravi

Reputation: 63

Java Selenium Webdriver - Loop issue when webelement is removed from html DOM at run time

I have application in which I need to approve webelement from the list there's total 50 item in the list Steps-

  1. Double click on first webelement and navigate to other page
  2. Now click on Approve button to approve it
  3. page will redirect to list page again.

Now problem is when I approve the element it get removed from list, and now my for loop skips the next element i;e 2 element from list , jumps to third element and approves it

in this scenario only 25 elements are getting approved rest remain in pending status

Any logic help would appreciate

Thanks, Ravi

Upvotes: 0

Views: 60

Answers (1)

Marit
Marit

Reputation: 3026

Without seeing your code, I'm going to assume that you are looping over the list and incrementing by one every time you go through the loop. And since the first element is removed from the list after approval, the second element becomes the first element. And because your loop increments by one, it goes to the second element in the list.

My suggestion would be to use a while {} instead of a for loop, which will select and approve the first element in the list while the size of the list is >= 1. That way you should get them all.

In pseudocode:

while (element.size >= 1) {
// approve element at first index
}

Upvotes: 1

Related Questions