TestProject Forum
Powered by leading experts in the test automation community

Selenium get element contains/not contains multiple classes with cssSelector

I’m trying to write a UI test with selenium on a FuelUX Repeater control,
i need to determine which one of the repeater-views is active and which is not.

Firstly i thought i would find the label element which has both classes ‘btn’ and ‘active’, and then i would search for another label element which has ‘btn’ class but does not have ‘active’.

I tried doing so via Css selector :

WebElement activeButton = repeaterViews.findElement(By.cssSelector("label.btn.active"));

Is it possible to find an element which does not have ‘active’ class ?

<div class="btn-group repeater-views" data-toggle="buttons">
     <label class="btn btn-default">
          <input name="repeaterViews" type="radio" value="list" class=""><span class="glyphicon glyphicon-list"></span>
     <label class="btn btn-default active">
          <input name="repeaterViews" type="radio" value="thumbnail" class=""><span class="glyphicon glyphicon-th"></span>

you would have a much easier time using Xpath.

WebElement inactiveButton = repeaterViews.findElement(By.xpath("//label[contains(@class, 'btn') and not(contains(@class, 'active'))]"));

This will select the other button element which does not have ‘active’ class on.