Alexander Borochkin
Alexander Borochkin

Reputation: 4641

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

I use Selenium WebDriver to scrape a table taken from a web page, written in JavaScript.

I am iterating on a list of table rows. Each row may be of a 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.

Upvotes: 41

Views: 58624

Answers (1)

alecxe
alecxe

Reputation: 474191

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

row.get_attribute("class")

Upvotes: 81

Related Questions