Alexander Alexander - 2 months ago 15
Python Question

How to get class name of the WebElement in Python Selenium?

I use Selenium webdriver to scrap a table taken from web page, written in JavaScript.
I am iterating on a list of table rows. Each row may be of different class. I want to get the name of this class, so that I can choose appropriate action for each row.

table_body=table.find_element_by_tag_name('tbody')
rows=table_body.find_elements_by_tag_name('tr')
for row in rows:
if(row.GetClassName()=="date"):
Action1()
else:
Action2()


Is this possible with Selenium? Or suggest another approach.

Answer

.get_attribute() method is what you are looking for:

row.get_attribute("class")