Reputation: 27
I am trying to locate and click an element which is having same className as other elements. I am unable to differentiate that element from other to click that element. Here is the HTML code of that element:
<a href="/category/men/N-fh7rea" class="accord-header">
Men
</a>
In this code, classname is same as others elements and text "Men" is also same. So made an Xpath of this:
//a[@class='accord-header' AND contains(text(),'Men') ]
Upvotes: 1
Views: 931
Reputation: 23
if you can't find any difference you can always count whith one, from the same objects interest you. If its for example 3rd element, save all of them as a list using findElements, and then get third element from it.
List<WebElement> elems = driver.findElements(By.xpath("//a[@class='accord-header' and @href='/category/men/N-fh7rea' and contains(.,'Men')]"));
WebElement elementThatYouLookedFor = elems.get(2);
If You need to click all of elements of this kind just use foreach loop:
List<WebElement> elems = driver.findElements(By.xpath("//a[@class='accord-header' and @href='/category/men/N-fh7rea' and contains(.,'Men')]"));
for(WebElement we : elems){
we.click(); //or any other operation
}
Upvotes: 0
Reputation: 193108
Tweak the xpath a bit and use :
//a[@class='accord-header' and @href='/category/men/N-fh7rea']
You can get more granular and use :
//a[@class='accord-header' and @href='/category/men/N-fh7rea' and contains(.,'Men')]
You can alos use :
//a[@class='accord-header' and @href='/category/men/N-fh7rea'][normalize-space()='Men']
Upvotes: 3