6

I have an element with a known id. I want to assert or verify that it has a specific class.

The HTML of the element is:

<a id="SearchList" class="something-else disabled"></a>

I want to use the id "SearchList" to locate the element and then verify that it has the class "disabled".

EDITS:

  • I am using the Selenium IDE a FireFox addon.
ism
  • 288
  • 1
  • 5
  • 17

4 Answers4

7
verifyElementPresent | css=a[id='SearchList'][class*='disabled'] | 
ism
  • 288
  • 1
  • 5
  • 17
3

This doesn't help you for the IED, but in C# I can do this using the GetAttribute method.

var allClasses = webElement.GetAttribute("class");
var elementHasClass = allClasses.Split(' ').Any(c => string.Equals("classLookingFor", c, StringComparison.CurrentCultureIgnoreCase));
AlignedDev
  • 8,102
  • 9
  • 56
  • 91
-1

You could use getAttribute() method to get the attribute and verify that. I assume you use Java for your scripts.

WebElement list = driver.findElement(By.id("SearchList"));
String disabled = list.getAttribute("disabled");

if(disabled.equals("disabled")){
  // something
}else{
  // something else
}
Purus
  • 5,701
  • 9
  • 50
  • 89
-1

Sorry for necroposting. I have some addition to what @Purus suggested.

As "disabled" is part of a class name class="something-else disabled", list.getAttribute("disabled") will return null. My suggestion to do it in the following way (Selenium WebDriver Java client):

WebElement element= driver.findElement(By.id("SearchList"));
String elementClass= element.getAttribute("class");

if(elementClass != null && elementClass.contains("disabled")){
  // something
}else{
  // something else
}
Dragon
  • 2,431
  • 11
  • 42
  • 68
  • this would pass with class="not-disabled". Just port Aligned's solution to Java if you want java. – Matthijs Wessels Jun 08 '16 at 10:53
  • Yes, it would pass "not-disabled", but TS asked to locate concrete class that is "disabled" and not "not-disabled". – Dragon Jun 08 '16 at 14:12
  • Let me reword: he wants to verify if the element has the class "disabled". If the element has any class that contains the text "disabled" (e.g. "not-disabled"), your code would act as if it has the class "disabled". This is wrong. – Matthijs Wessels Jun 08 '16 at 14:59