Getting multiple ‘driver.find_elements_by_xpath’ as .text at once – Selenium – Python

Is there a way to get multiple ‘.get_attribute(“text”)’ at once, it seems to only work with getting multiple element ID’s at once.

Works

locationAndType = driver.find_elements_by_xpath("//ul/li/a/span")

Doesn’t work

locationAndType = driver.find_elements_by_xpath("//ul/li/a/span").get_attribute("text")

Answer

find_elements_by_xpath return a list. But you can use a list comprehension to unpack the attribute in one expression:

locationAndType = [elem.get_attribute("text") 
                   for elem in driver.find_elements_by_xpath("//ul/li/a/span")]