Reputation: 81
I have a collection of item on a web page and all items have the same class. The elements do not have IDs. When I use class name, it will pick only the first item, but I want click on the second item. How can i click .
My code :
WebElement element = driver.findElement(By.className("item-group-list"));
element.click();
Upvotes: 1
Views: 2366
Reputation: 5347
You can use findElements method to get all elements then click on second item as given below.
List<WebElement> lstElements = driver.findElements(By.className("item-group-list"));
lstElements.get(1).click();
or You can try with x-path (//*[@class='item-group-list'])[2]
to get the second element directly as follows.
WebElement element = driver.findElement(By.xpath("(//*[@class='item-group-list'])[2]")); //index starts with 1 here
element.click();
Upvotes: 1