TestProject Forum
Powered by leading experts in the test automation community

Check if class exists on a bootstrap button Selenium Java

I have a twitter bootstrap button on which I need to automate a click

How can I best check if a button element is enabled or not?

<button id="myBtn" type="button" class="disabled btn btn-default btn-lg"> <span class="glyphicon glyphicon-star" aria-hidden="true"></span> Demos </button>

I was thinking about getting the css class but it didn’t work
any idea how I can achieve this ?

WebElement btn = driver.findElement(By.id("myBtn")); btn.getCssValue("class");

I think the best way to do it would be with WebElement.getAttribute()

String classes = btn.getAttribute("class");

This will return a string of all classes present on your button element.
So the string will look something like: "disabled btn btn-default btn-lg"

And then you can check if it has the ‘disabled’ class in there:

if(classes.contains(“disabled”)) {

  // logic