Reputation: 21
I need to find number of anchor tags
and their titles
in code mentioned below. I am using web driver, java, Selenium.
<div>
<div>
<div>
<div>
<ul>
<li>
<a></a>
</li>
<li>
<a></a>
</li>
</ul>
<ul>
<li>
<a></a>
</li>
<li>
<a></a>
</li>
</ul>
<ul>
<li>
<a></a>
</li>
<li>
<a></a>
</li>
</ul>
</div>
</div>
</div>
</div>
What will be the best way to find it?
Thanks in advance.
Upvotes: 0
Views: 2574
Reputation: 3776
I assume that by 'title' you meant the text between the anchor tags. Use the following code:-
//find the div tag
WebElement divTag = driver.findElement(By.xpath("//div/div/div/div"));
//find all the a tags in the div tag
List<WebElement> allAnchors = divTag.findElements(By.tagName("a"));
//print number of anchor tag
System.out.println("Number of Anchor tags = " + allAnchors.size());
//print text within each anchor tag
int count=0
For(WebElement anchor : allAnchors) {
System.out.println("Text within anchor"+ (++count) + "="+ anchor.getText());
}
Let me know if this helps you.
Upvotes: 1