Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 265
Alexander Avatar asked Sep 25 '16 15:09

Alexander


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 select a class in Selenium Python?

We can select a drop-down menu option value with Selenium webdriver. The Select class in Selenium is used to handle drop-down. In an html document, the drop-down is identified with the <select> tag. Let us see the html structure of a drop-down.

Is WebElement a class?

This class provides a mechanism to represent them as objects & perform various actions on the related elements. Typically, the findElement method in remoteDriver returns an object of class webElement.


1 Answers

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

row.get_attribute("class")
like image 50
alecxe Avatar answered Sep 27 '22 21:09

alecxe