dcraven
dcraven

Reputation: 139

Extract data using find_elements_by_class_name

I am trying to extract each class_name='position-header' on the page by using find_elements_by_class_name but when I do so I receive the error:

AttributeError: 'list' object has no attribute 'text'

from parsel import Selector  
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys   

employment = driver.find_elements_by_class_name('position-header')

The screenshot shows that it returns data when the method find_element_by_class_name is used but when using find_elements_by_class_name I encounter the error.

enter image description here

Upvotes: 0

Views: 3433

Answers (1)

Hybrid
Hybrid

Reputation: 7049

The driver.find_elements_by_class_name() method returns a list of matching elements, and you are trying to access the text attribute on the list, which doesn't exist... text is only available on a single instance of element.

You would either have select the first one and access the .text attribute:

driver.find_elements_by_class_name('position-header')[0].text

or iterate over the list and get the ext of each element:

elements = driver.find_elements_by_class_name('position-header')
for element in elements:
    print(element.text)

Upvotes: 4

Related Questions