

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 get css class name using Selenium?
We can get the css class name of an element with Selenium webdriver. To obtain the class name attribute of an element in the html document, we have to use the getAttribute() method. Then the class value is passed as a parameter to the method.
Let us consider the below html code with class attribute.
The class attribute is having the value as gsc-input. This can be obtained with the help of getAttribute() method.
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 GetClssAttribute{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "https://www.tutorialspoint.com/index.htm"; driver.get(url); driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS); // identify element WebElement l = driver.findElement(By.id("gsc-i-id1")); // get class attribute with getAttribute() String val = l.getAttribute("class"); System.out.println("Class attribute value: " + val); driver.quit() } }
Output
- Related Questions & Answers
- Selenium Webdriver Locating Strategies By Class Name
- How to find an element using the attribute “class name” in Selenium?
- How to get the content of href within some targeted class using selenium?
- How to get details of a webpage like url, title name, domain name etc using Javascript Executor in Selenium?
- Using predefined class name as Class or Variable name in Java
- How to get the class name of an instance in Python?
- How to capture tooltip in Selenium using Actions Class?
- Get Canonical Name for a class in Java
- How to get content of entire page using Selenium?
- How to get HTTP Response Code using Selenium WebDriver?
- Get the unqualified name of a class in Java
- Get the class name for various objects in Java
- How to get Resource name using Resource id in Android?
- How to get the Azure VM DNS name using PowerShell?
- How to get Resource Name using Resource id in Android using Kotlin?
Advertisements