Selenium – Toggle button click using selenium webdriver

selenium

Having multiple toggle button but same class name for each toggle button.

Code here

<div class="col-md-10 blockBox">
<h4 class="panel-title">
<a class="accordion-toggle collapsed" href="#CompetencyBlock" data-parent="#CompetencyAccordion" data-toggle="collapse">
Forms And Assessments For Individuals
<div class="tabHeaderImage"></div>
</a>
</h4>
</div>


<div class="col-md-10 blockBox">
<h4 class="panel-title">
<a class="accordion-toggle collapsed" href="#BirthDateBlock" data-parent="#BirthDateAccordion" data-toggle="collapse">
Birthdate
<div class="tabHeaderImage"></div>
</a>
</h4>
</div>

I tried these code

1)  List<WebElement> text = driver.findElements(By.className("panel-title"));
       text.get(2).click();


2)  List<WebElement> text = driver.findElements(By.className("tabHeaderImage"));
       text.get(2).click();

But i am able click first four toggle button only remaining toggle button i am not able click

Best Answer

List<WebElement> text = driver.findElements(By.xpath("//h4[@class='panel-title']/following-sibling::a"));
text.get(0).click();

you should locate anchor tag because anchor tag only is clickable..div and h4 tags are not clickable