Search code examples
pythonseleniumgetattribute

Python Selenium Webdriver: how to select more than one element using get_attribute


I'm trying to find the urls on a given page using the following:

driver.find_element_by_css_selector('.listing-title a').get_attribute('href')

This works well enough to grab the first url. However, I can't figure out how to get the other href attributes on the page. I tried adding an s to element:

driver.find_elements_by_css_selector('.listing-title a').get_attribute('href')

But I get the following error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'get_attribute'

Any advice?


Solution

  • I figured it out, find_elements_by_css_selector returns a list so I need to loop over it:

    x = driver.find_elements_by_css_selector('.listing-title a')
    for each in x:
        print each.get_attribute('href')