- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use selenium to check if element contains specific class attribute?
We can check if an element contains a specific class attribute value. The getAttribute() method is used to get the value of the class attribute. We need to pass class as a parameter to the getAttribute() method.
First of all we need to identify the element with help of any of the locators like id, class, name, xpath or css. Then obtain the class value of the attribute. Finally, we need to check if the class attribute contains a specific value.
Let us take an element with the below html code having class attribute. The tp-logo is the value of the class attribute.
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class SpecificClassVal{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.tutorialspoint.com/about/about_careers.htm"); // identify element WebElement t=driver.findElement(By.xpath("//img[@class='tp-logo']")); // get class attribute with getAttribute() String clsVal = t.getAttribute("class"); //iterate through class value for (String i : clsVal.split(" ")) { //check the class for specific value if (i.equals("tp-logo")) { System.out.println("class attribute contains: " + clsVal); } else { System.out.println("class attribute does not contain: " + clsVal);} } driver.close(); } }
Output
- Related Articles
- How to find an element using the attribute “class name” in Selenium?
- How to select element with specific class and title attribute using jQuery?
- Check if the SortedSet contains a specific element in C#
- How to check if the string contains the specific word?
- How to check if a string contains a specific sub string?
- How to get attribute of element from Selenium?
- How to check if Element exists in c# Selenium drivers?
- How to check if a slice contains an element in Golang?
- How to check if Java list contains an element or not?
- How to test if an element contains class in JavaScript in HTML?
- How to use relative xpath for locating a web-element by particular Attribute in Selenium?
- How do I find an element that contains specific text in Selenium Webdriver?
- How to check if dom has a class using WebDriver (Selenium 2)?
- How to check if an element has a class in jQuery?
- How do I find an element that contains specific text in Selenium WebDriver (Python)?

Advertisements