简体   繁体   中英

Selenium assert has class

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.
verifyElementPresent | css=a[id='SearchList'][class*='disabled'] | 

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));

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
}

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM