Showing posts with label Consider there are two elements in a page with same id. How will you locate the second element. Show all posts
Showing posts with label Consider there are two elements in a page with same id. How will you locate the second element. Show all posts

Wednesday, February 14, 2018

How to click the 'Second' element out of '8' matching element using Selenium Webdriver (Two elements in a page with same ID's)

Option 1:

You can use XPath indexing.

Example:
driver.findElement(By.xpath("(//*[@id="notificationStatusSwitch"])[2]"));

Option 2:

You can use findElements(): Using the given “locating mechanism” find all the elements on the current page.It returns a list of web elements, Then access the second one found

Example:

List<WebElement> elementList = driver.findElements(By.xpath("//*[@id="notificationStatusSwitch"]"));

elementList.get(1).click;