Get the text from multiple elements with the same class in Selenium for Python?

find_element_by_xpath returns one element, which has text attribute.

find_elements_by_xpath() returns all matching elements, which is a list, so you need to loop through and get text attribute for each of the element.

all_spans = driver.find_elements_by_xpath("//span[@class='class']")
for span in all_spans:
    print span.text

Please refer to Selenium Python API docs here for more details about find_elements_by_xpath(xpath).


This returns a list of items:

all_spans = driver.find_elements_by_xpath("//span[@class='class']")
for span in all_spans:
    print span.text