0

Is there a way to get the CSS class that has been applied to the selected WebElement, we do have the getCssValue method, however it provides the value of the specific attribute and not the class that has been applied to the WebElement

1

3 Answers 3

1

Use getAttribute(attributeLocator) function,

Specify the Xpath of the element for the class you needed to retrieve.

  selenium.getAttribute(//xpath@class);
Sign up to request clarification or add additional context in comments.

1 Comment

The question is tagged WebDriver, this is Selenium RC/v1 code.
1

First get the WebElement using any locator like Xpath,id etc., Now apply get attribute method on the WebElement as below:

WebDriver driver=new WebDriver();
WebElement element=driver.findelement(Bylocator);
String className=element.getattribute("class");

This method can be used to get any attributes of a particular WebElement like href attribute of a link etc.

Example:

WebDriver driver=new FirefoxDriver();
driver.get("http://www.gmail.com");
WebElement element=driver.findElement(By.id("Email"));
System.out.println("Placeholder of email text box: "+ element.getAttribute("placeholder"));

The above code will print the placeholder displayed in the Email box.

Output displayed in the console:

Placeholder of email text box: Enter your email

Comments

-3

I think the Chrome Debug Tool is helpful (Press F12 to access it)

Right click on an element and Inspect it. In the Styles tag , the classes are shown with match css rules!

2 Comments

This question relates to Selenium Webdriver
@user2428562, it always helps to read the question before responding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.