Disclaimer: I went through the answers here. But would like more insight on situations similar to the code below.
<div id="feedback-filter-buttons" class="ui-buttonset">
<label class="filter-label ui-button ui-corner-top" for="feedback-all" role="button">
<span class="ui-button-text">Very Good</span>
</label>
<label class="ui-button" role="button">
<span class="ui-button-text"><span > Good </span></span>
</label>
<label class="ui-button" role="button">
<span class="ui-button-text"><span > Alright </span></span>
</label>
<label class="ui-button ui-state-active" role="button">
<span class="ui-button-text"><span > Bad</span></span>
</label>
<label class="ui-button" role="button">
<span class="ui-button-text"><span > Very Bad</span></span>
</label>
<label class="ui-button ui-corner-bottom" role="button">
<span class="ui-button-text"><span > Undefined (0)</span></span>
</label>
</div>
Objective:
To get the text of the currently selected (ie., active) label(button).
Approaches:
a) webDriver.findElement(By.id("feedback-filter-buttons")).findElement(By.className("ui-state-active")).getText()
b) webDriver.findElement(By.xpath("//*[@id='feedback-filter-buttons']/label[contains(@class,'ui-state-active')]/span")).getText()
Question:
Which of the above approaches is better and why? At the moment, I am leaning towards xpath as I am able to specify the path till the final element I require.