如何在 python 上使用 selenium 获取页面中的属性?

How can i get an attribute in a page using seleinum on python?

我正在尝试获取一页元素源的内容

from selenium import web driver 
firefox = webdriver.Firefox()
firefox.get('some link right here')
linkimg = firefox.find_elements_by_xpath('/html/body/div[3]/div/div/img').get_attribute('src')

但是当我 运行 这段代码时它说:AttributeError: 'list' object has no attribute 'get_attribute'

我试过 python 3.7 和 3.9

.find_elements_by_xpath() 的输出始终是找到的元素的列表。调用 .get_attribute() 对列表没有意义。相反,遍历列表并获取您的 src 属性。

linkimg = firefox.find_elements_by_xpath('/html/body/div[3]/div/div/img')
linkimg = [each_element.get_attribute('src') for each_element in linkimg]