Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get css class name using Selenium?

I am new in selenium testing. I want to get the css class name using selenium. I am using eclipse and Java for development.

<table > <tr class="odd"><td>Odd row</td></tr> <tr class="even"><td>Even row</td></tr> <tr class="odd"><td>Odd row2</td></tr> <tr class="even"><td>Even row2</td></tr>        </table> 

Is there any way to get the class name 'odd' or 'even' using selenium? I

like image 385
maruf571 Avatar asked Oct 10 '11 13:10

maruf571


People also ask

How do I find class name in Selenium?

We can find an element using the attribute class name with Selenium webdriver using the locators - class name, css, or xpath. To identify the element with css, the expression should be tagname[class='value'] and the method to be used is By. cssSelector.

How do I retrieve css properties in Selenium?

Type “css=input[type='submit']” (locator value) in Selenium IDE. Click on the Find Button. The “Sign in” button will be highlighted, verifying the locator value.

What is css locator in Selenium?

What are CSS Selectors in Selenium? CSS Selectors are one of the locator strategies offered by Selenium to identify the web elements. The CSS Selectors mainly use the character sequence pattern, which identifies the web elements based on their HTML structure.


2 Answers

From a WebElement you can use the getAttribute method like this:

element.getAttribute("class") 
like image 96
Stephen Avatar answered Oct 05 '22 07:10

Stephen


Yes, you can use getAttribute(attributeLocator) function for the your requirement.

 selenium.getAttribute(//xpath@class); 

Specify the Xpath of the element for which you require to know the class of.

Thanks.

like image 40
lAH2iV Avatar answered Oct 05 '22 07:10

lAH2iV