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.
I have try 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]
!!!
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]
).
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".
https://docs.katalon.com/katalon-studio/docs/detect_elements_xpath.html#what-is-xpath