【问题标题】:AttributeError: 'list' object has no attribute 'get_attribute'AttributeError:“列表”对象没有属性“get_attribute”
【发布时间】:2021-10-09 13:53:46
【问题描述】:

我有这样一部分代码,并希望接收我在“statistic_info_link”中找到的元素的链接。 但它给了我错误并写:

AttributeError: 'list' 对象没有属性 'get_attribute'

这是带有JS元素的html页面结构http://www.ukrstat.gov.ua/head.html 因此,我需要找到写有“统计信息”的链接-“Статистична інформація”

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)

感谢您的帮助!

【问题讨论】:

    标签: python html selenium web-scraping


    【解决方案1】:

    由于您提供browser.find_elements,它会返回 List 中的元素。您可以将.get_attribute 用于 WebElement 而不是 Webelements 列表。

    要么将statistic_info_link = browser.find_elements 更改为

    statistic_info_link = browser.find_element...
    

    或者遍历列表

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

    【讨论】:

      【解决方案2】:
      AttributeError: 'list' object has no attribute 'get_attribute'
      

      错误说明您试图在 Python 中的列表上调用 get_attribute,但它应该是一个 Web 元素。

      记住find_elements(复数)在Python-Selenium 绑定 中返回一个列表。 find_elementPython-Selenium 绑定

      中返回一个 Web 元素

      问题解决方案:

      1. 在大多数情况下,更改为 find_element 应该可以解决问题。

      所以,而不是

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

      这样做:

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

      另外,我不太确定outerHTML,因此我将其更改为innerHTML

      第二次修复:

      使用列表索引:

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

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

      最后,如果您真的想从列表中提取文本,请使用以下代码:

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

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

      【讨论】:

        猜你喜欢
        • 2021-10-10
        • 1970-01-01
        • 1970-01-01
        • 2021-07-05
        • 2016-07-28
        • 2018-01-16
        • 2016-05-14
        • 2016-12-21
        • 2022-01-23
        相关资源
        最近更新 更多