user3685742 user3685742 - 15 days ago 5
Python Question

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

I'm trying to scrape data from a page with JavaScript loaded content. For example, the content I want is in the following format:

<span class="class">text</span>
...
<span class="class">more text</span>


I used the
find_element_by_xpath(//span[@class="class"]').text
function but it only returned the first instance of the specified class. Basically, I would want a list like
[text, more text]
etc. I found the
find_elements_by_xpath()
function, but the
.text
at the end results in an error
exceptions.AttributeError: 'list' object has no attribute 'text'
.

Answer

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).