Question: I have a `<ul>`, which has many `<li>` , each of which contains an anchor tag. The xpath of the list elements looks like the one:

`String xpath = "//*[@id='page']/section[8]/div/div/div[2]/div/ul/li[";`

`String part2 = "]/a";`

My task involves clicking on each of those links, coming back to the homepage, and then clicking on another link.

At this moment, my webpage contains 5 such elements. To click on each element, I repeat the following inside a for loop:

`driver.findElement(By.xpath(part1 + i + part2)).click();` followed by `driver.navigate().back();`

The problem is I have hardcoded the value 5. It can be 6 some other day, or can have any number of links anyday.

What logic can I use to check the size of the ul? **The problem is that I cannot use a `java.util.List` to store the links beforehand.**

Answer: 
Are you allowed to store their count? :)

```
int len = driver.findElements(By.xpath("//*[@id='page']/section[8]/div/div/div[2]/div/ul/li/a").size()

```

`findElements` will find all elements matching the locator (note the plural), return as a List, on which you you get the size (length).

So now you'll know the upper bound of the loop.
Since you mentioned each `<li>` tag has one anchor tag. That means the number of `<li>` and `<a>` tags are same. You can use `driver.findElements` to get a list of all `<li>` tags and save them in an ArrayList and can easily get the size of the `<li>` tags. This value would be the iterator value for you in the loop you are already using.