Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the class name through selenium

Tags:

selenium

 <table >
<tr class="odd First"><td>1one Cell</td><td>2one Cell</td><td>3one Cell</td><td>4one Cell</td> </tr>
<tr class="even Second"><td>Two Cell</td><td>2Two Cell</td><td>3Two Cell</td><td>4Two Cell</td></tr>
<tr class="odd Thrid"><td>1Three Cell</td><td>2Three Cell</td><td>3Three Cell</td><td>4Three Cell</td></tr>
<tr class="even Fourth"><td>1Five Cell</td><td>2Five Cell</td><td>3Five Cell</td><td>4Five Cell</td></tr>       
</table>

How can i get the class names of the tr. Please suggest me.

like image 380
srini Avatar asked Jun 29 '12 10:06

srini


1 Answers

To get the class names of all the tags using java.

List<WebElement> list = driver.findElements(By.tagName("tr"));

for(WebElement ele:list){
   String className = ele.getAttribute("class");
   System.out.println("Class name = "+className);
}

This will print all the class names to the console for all the tags on the web page.

String className = selenium.getAttribute("//html/body/table/tbody/tr[1]/@class");

May be this code might get you the value of the first tag's class name. Let me know if this works.

like image 118
Hari Reddy Avatar answered Jan 01 '23 09:01

Hari Reddy