Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Selenium WebDriver find_elements_by_class_name

I want to select all the rows in a table that have class name either TCP_RowOdd or TCP_RowEven. Currently, I am doing it like this

oddRows = driver.find_elements_by_class_name("TCP_RowOdd")

evenRows = driver.find_elements_by_class_name("TCP_RowEven")

Is there an OR clause that can be used here to do it in a single query.

like image 816
shantanu Avatar asked Feb 15 '16 00:02

shantanu


People also ask

What does Find_elements_by_class_name return?

The first element with the link text value matching the location will be returned. The first element with the partial link text value matching the location will be returned. The first element with the given tag name will be returned. the first element with the matching class attribute name will be returned.

What does the method Find_element_by_class_name do in Python?

The . find_element_by_class_name() method only returns the first element with the matching class. It raises a NoSuchElementException if no element exists with the given class name.

How do I find my Selenium ID?

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

What are the exception in Selenium with Python?

Exceptions in Selenium Python are generally classified into two types. Checked Exception handling in Selenium is conducted while writing the code. On the other hand, Unchecked Exceptions are raised during runtime and can generate fatal errors compared to the former type of exception.


1 Answers

There are multiple ways to that, I prefer the CSS selector:

rows = driver.find_elements_by_css_selector(".TCP_RowOdd,.TCP_RowEven")

The comma in the selector means "or".


Or, we can grab all elements having a class that starts with TCP_Row:

rows = driver.find_elements_by_css_selector("[class^=TCP_Row]")
like image 116
alecxe Avatar answered Oct 05 '22 23:10

alecxe