'list' object has no attribute 'get_attribute'

href = song.find_elements(By.XPATH,".//span[@class='txt']/a").get_attribute("href")
报错如下:
'list' object has no attribute 'get_attribute'

song.find_elements 返回的是一个列表但列表是没有 get_attribute方法的

获取列表中的第一个元素,然后调用对应方法

elements = song.find_elements(By.XPATH,".//span[@class='txt']/a")
for element in elements:
    href = element.get_attribute("href")
    # do something with href

可行还望采纳:
这个错误说明查询到的元素是一个列表,但是你试图在列表上使用 get_attribute 方法。您需要对列表中的元素进行遍历,然后对每个元素调用 get_attribute 方法。

elements = song.find_elements(By.XPATH,".//span[@class='txt']/a")
for element in elements:
    href = element.get_attribute("href")