从Selenium for Python中具有相同类的多个元素中获取文本?

从Selenium for Python中具有相同类的多个元素中获取文本?,python,selenium,xpath,selenium-webdriver,webdriver,Python,Selenium,Xpath,Selenium Webdriver,Webdriver,我正试图从一个包含JavaScript加载内容的页面中抓取数据。例如,我想要的内容的格式如下: <span class="class">text</span> ... <span class="class">more text</span> 文本 ... 更多文本 我使用了find\u element\u by_xpath(//span[@class=“class”]”)。text函数,但它只返回指定类的第一个实例。基本上,我想要一个类似[te

我正试图从一个包含JavaScript加载内容的页面中抓取数据。例如,我想要的内容的格式如下:

<span class="class">text</span>
...
<span class="class">more text</span>
文本
...
更多文本

我使用了
find\u element\u by_xpath(//span[@class=“class”]”)。text
函数,但它只返回指定类的第一个实例。基本上,我想要一个类似
[text,more text]
等的列表。我找到了
find_elements\u by_xpath()
函数,但是
.text
最后会导致一个错误
异常。AttributeError:'list'对象没有属性“text”
find_element\u by_xpath
返回一个元素,具有
text
属性的

find\u elements\u by\u xpath()
返回所有匹配的元素,这是一个列表,因此您需要遍历并获取每个元素的
text
属性

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

有关通过xpath(xpath)查找元素的更多详细信息,请参阅Selenium Python API文档