Selenium assert has a class

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

HTML element:

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

I want to use the identifier "SearchList" to find the item, and then check that it has the class "disabled".

edits:

  • I am using the Selenium IDE to complement FireFox.
+4
source share
4 answers
verifyElementPresent | css=a[id='SearchList'][class*='disabled'] | 
+6
source

This will not 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));
+2
source

getAttribute() . , Java .

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

if(disabled.equals("disabled")){
  // something
}else{
  // something else
}
-1

. , @Purus.

Since "disabled" is part of the class name class="something-else disabled", it list.getAttribute("disabled")will return null. My suggestion is to do this as follows (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
}
-1
source

Source: https://habr.com/ru/post/1545264/


All Articles