Search code examples
pythonselenium-webdrivergeckodriver

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


I am trying to get the content of the element source of one page

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')

but when i run this code it says: AttributeError: 'list' object has no attribute 'get_attribute'

Im tried on python 3.7 and 3.9


Solution

  • The output of .find_elements_by_xpath() is always a list of the found elements. Calling .get_attribute() does not make sense for a list. Instead, iterate over the list and get your src attributes instead.

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