How can I get the `href` attributes of nested `<a>` from specific `<ul>` into a list?

I have a <ul> with xpath:position = //ul[5]. Which contains some <a>.

The first <a> has xpath:position = //ul[5]/li/div/div/a
The next <a> has xpath:position = //ul[5]/li[2]/div/div/a
and the next has xpath:position = //ul[5]/li[3]/div/div/a
and goes on…

So, for every new <a> into this <ul> the xpath:position of <a> get a [#] after <li>.

What I need is an example of how I’ll count how many <a> exist into this specific <ul> and then get the href attribute of each <a> into a list.

Thank you for your time!!!

─────[ EDIT ]─────

I am trying this:

	WebDriver driver = DriverFactory.getWebDriver()
	def aCount = driver.findElements(By.xpath("//ul[5]/li/div/div/a")).size()
	println aCount

But it counts all the <a> of the page and not only the ones withing the <ul> with xpath:position = //ul[5]!!!

Can you share an example of <a>'s xpath that should not be taken? It seems that they may have same identifier that you are looking for //ul[5]/li/div/div/a

There is 5 difrent <ul>s in this page and into every one of this <ul> there is some <a>s. And I want to get the hrefs only from <a>s of 5th <ul>. Here is an example of their xpath:

//div[@id='page_naboo_artist']/div[2]/div/div/div/ul
//div[@id='page_naboo_artist']/div[2]/div/div/div/ul/li/div/div/a
//div[@id='page_naboo_artist']/div[2]/div/div/div/ul/li[2]/div/div/a
//div[@id='page_naboo_artist']/div[2]/div/div/div/ul/li[3]/div/div/a
and goes on...

//div[@id='page_naboo_artist']/div[2]/div/div[2]/div/ul[2]
//div[@id='page_naboo_artist']/div[2]/div/div[2]/div/ul[2]/li/div/div/a
//div[@id='page_naboo_artist']/div[2]/div/div[2]/div/ul[2]/li[2]/div/div/a
//div[@id='page_naboo_artist']/div[2]/div/div[2]/div/ul[2]/li[3]/div/div/a
and goes on...

//div[@id='page_naboo_artist']/div[2]/div/div[2]/div/ul[3]
//div[@id='page_naboo_artist']/div[2]/div/div[2]/div/ul[3]/li/div/div/a
//div[@id='page_naboo_artist']/div[2]/div/div[2]/div/ul[3]/li[2]/div/div/a
//div[@id='page_naboo_artist']/div[2]/div/div[2]/div/ul[3]/li[3]/div/div/a
and goes on...

//div[@id='page_naboo_artist']/div[2]/div/div[2]/div/ul[4]
//div[@id='page_naboo_artist']/div[2]/div/div[2]/div/ul[4]/li/div/div/a
//div[@id='page_naboo_artist']/div[2]/div/div[2]/div/ul[4]/li[2]/div/div/a
//div[@id='page_naboo_artist']/div[2]/div/div[2]/div/ul[4]/li[3]/div/div/a
and goes on...

//div[@id='page_naboo_artist']/div[2]/div/div[2]/div/ul[5]
//div[@id='page_naboo_artist']/div[2]/div/div[2]/div/ul[5]/li/div/div/a
//div[@id='page_naboo_artist']/div[2]/div/div[2]/div/ul[5]/li[2]/div/div/a
//div[@id='page_naboo_artist']/div[2]/div/div[2]/div/ul[5]/li[3]/div/div/a
and goes on...

I finally found the solution!!! :sweat_smile:

The problem was that, into the //ul[5] there were two kind of <a>s. The //ul[5]/li/div/div/a and the //ul[5]/li/div/div[2]/a.

At the first case the <div> which wraps the <a> has the class name (div[@class="heading-4"]/a[1]).
At the second case the <div> which wraps the <a> has the class name (div[@class="heading-4-sub"]/a[1]).

And when I was counting the <a>s I was getting both kind of <a>s in count.

So I had to do something like this:

WebDriver driver = DriverFactory.getWebDriver()

List<String> hrefs = []
List<WebElement> aTags = driver.findElements(By.xpath('//ul[5]/li/div/div[@class="heading-4"]/a'))

for (WebElement aTag in aTags) {
	String href = aTag.getAttribute("href")
	if (href != null) {
		hrefs.add(href);
	} else {
		hrefs.add('Empty Link');
	}
}
System.out.println(hrefs + "\n\nURLs Found: " + hrefs.size())

I was using:
findElements(By.xpath("//ul[5]/li/div/div/a"))
Instead of:
findElements(By.xpath('//ul[5]/li/div/div[@class="heading-4"]/a')) which gets only the <a>s which are wrapped by a <div> with class name “heading-4”.

PS: If anyone has a better solution/idea or something which will make this code better, please let me know!!!