Home > Blockchain >  AttributeError: 'list' object has no attribute 'get_attribute'
AttributeError: 'list' object has no attribute 'get_attribute'

Time:10-10

I have such a part of code and want to receive link on element that I find in "statistic_info_link". But it gives me error and write :

AttributeError: 'list' object has no attribute 'get_attribute'

Here is structure of html page with JS elements http://www.ukrstat.gov.ua/head.html As a result I need to find link where is written "Statistic information"-"Статистична інформація"

r = requests.get("http://www.ukrstat.gov.ua/") 
soup = BeautifulSoup(r.text) 
head=soup.find('frame',{'name': 'banner'})
#Recieve link on head
link_head='http://www.ukrstat.gov.ua/' head.get('src')
browser.get(link_head)
statistic_info_link = browser.find_elements_by_xpath("//*[contains(text(), 'Статистична інформація')]").get_attribute("outerHTML")
print(statistic_info_link)

Thanks for help!

CodePudding user response:

You need for loop to get string:

r = requests.get("http://www.ukrstat.gov.ua/") 
soup = BeautifulSoup(r.text) 
head=soup.find('frame',{'name': 'banner'})
#Recieve link on head
link_head='http://www.ukrstat.gov.ua/' head.get('src')
browser.get(link_head)
statistic_info_link = browser.find_elements_by_xpath("//*[contains(text(), 'Статистична інформація')]")
for i in statistic_info_link:
    print(i.get_attribute("outerHTML"))

CodePudding user response:

Since you are giving browser.find_elements it's returning the element in a List. And you can use .get_attribute to a WebElement not to list of Webelements.

Either change statistic_info_link = browser.find_elements to

statistic_info_link = browser.find_element...

Or iterate over the list

statistic_info_link = browser.find_elements_by_xpath("//*[contains(text(), 'Статистична інформація')]")
for ele in statistic_info_link:
    print(ele.get_attribute("outerHTML"))

CodePudding user response:

AttributeError: 'list' object has no attribute 'get_attribute'

The error explains that you are trying to invoke get_attribute on a list in Python, but it should have been a web element.

Remember find_elements (plural) returns a list in Python-Selenium bindings. Where as find_element returns a web element in Python-Selenium bindings

Problem solution :

  1. In most cases changing to find_element should fix the issue.

So, Instead of

statistic_info_link = browser.find_elements_by_xpath("//*[contains(text(), 'Статистична інформація')]").get_attribute("outerHTML")
print(statistic_info_link)

do this :

statistic_info_link = browser.find_element_by_xpath("//*[contains(text(), 'Статистична інформація')]").get_attribute("innerHTML")
print(statistic_info_link)

Also, I am not really sure about outerHTML, hence I have changed that to innerHTML.

Second Fix :

Use the list index :

statistic_info_link = browser.find_elements_by_xpath("//*[contains(text(), 'Статистична інформація')]")[0].get_attribute("outerHTML")
print(statistic_info_link)

or

statistic_info_link = browser.find_elements_by_xpath("//*[contains(text(), 'Статистична інформація')]")[0].get_attribute("innerHTML")
print(statistic_info_link)

lastly if you really want to extract text from a list please use below code :

statistic_info_link = browser.find_elements_by_xpath("//*[contains(text(), 'Статистична інформація')]").get_attribute("outerHTML")
for ele in statistic_info_link :
    print(ele.get_attribute("outerHTML"))

or

statistic_info_link = browser.find_elements_by_xpath("//*[contains(text(), 'Статистична інформація')]").get_attribute("outerHTML")
for ele in statistic_info_link :
    print(ele.get_attribute("innerHTML"))
  • Related