Check if class exists on a bootstrap button Selenium Java

  • 1 Replies
  • 1373 Views
Check if class exists on a bootstrap button Selenium Java
« on: February 15, 2016, 13:59:47 pm »
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?

Code
<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 ?

Code
WebElement btn = driver.findElement(By.id("myBtn"));
btn.getCssValue("class");
« Last Edit: February 15, 2016, 14:15:56 pm by Walter »

Re: Check if class exists on a bootstrap button Selenium Java
« Reply #1 on: February 15, 2016, 18:56:11 pm »
I think the best way to do it would be with WebElement.getAttribute()

Code
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:
Code
"disabled btn btn-default btn-lg"

And then you can check if it has the 'disabled' class in there:
Code
if(classes.contains("disabled")) {

      // logic
}
« Last Edit: February 15, 2016, 18:58:54 pm by Luthar »

 

Sitemap 1 2 3 4 5 6 7 8