user3685742 user3685742 - 1 year ago 78
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
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
function, but the
at the end results in an error
exceptions.AttributeError: 'list' object has no attribute 'text'

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download