遍历 WebElements 时,“list”对象没有属性“get_attribute”

2025-04-15 09:19:00
admin
原创
69
摘要:问题描述:我正在尝试使用 Python 和 Selenium 抓取网页上的多个链接。我find_elements_by_xpath能够找到一个元素列表,但无法将返回的列表更改为实际的href链接。我知道find_element_by_xpath这种方法有效,但仅适用于单个元素。这是我的代码:path_to_c...

问题描述:

我正在尝试使用 Python 和 Selenium 抓取网页上的多个链接。我find_elements_by_xpath能够找到一个元素列表,但无法将返回的列表更改为实际的href链接。我知道find_element_by_xpath这种方法有效,但仅适用于单个元素。

这是我的代码:

path_to_chromedriver = 'path to chromedriver location'
browser = webdriver.Chrome(executable_path = path_to_chromedriver)

browser.get("file:///path to html file")

all_trails = []

#finds all elements with the class 'text-truncate trail-name' then 
#retrieve the a element
#this seems to be just giving us the element location but not the 
#actual location

find_href = browser.find_elements_by_xpath('//div[@class="text truncate trail-name"]/a[1]')
all_trails.append(find_href)

print all_trails

此代码返回:

<selenium.webdriver.remote.webelement.WebElement 
(session="dd178d79c66b747696c5d3750ea8cb17", 
element="0.5700549730549636-1663")>, 
<selenium.webdriver.remote.webelement.WebElement 
(session="dd178d79c66b747696c5d3750ea8cb17", 
element="0.5700549730549636-1664")>,

我希望该all_trails数组是一个链接列表,例如:www.google.com, www.yahoo.com, www.bing.com

我尝试循环遍历all_trails列表并在列表上运行该get_attribute('href')方法,但出现错误:

错误图片

有谁知道如何将 selenium WebElement 转换为 href 链接?

任何帮助都将不胜感激:)


解决方案 1:

让我们看看你的代码中发生了什么:

在没有任何可见性的情况下,HTML似乎以下行返回两个WebElementsList find_href它们又被附加到:all_trails List

find_href = browser.find_elements_by_xpath('//div[@class="text truncate trail-name"]/a[1]')

因此,当我们打印时,List all_trails两者WebElements都会打印。因此没有错误。

根据您提供的错误快照,您正在尝试调用不受支持get_attribute("href")方法。因此您看到以下错误:List

'List' Object has no attribute 'get_attribute'

解决方案 :

为了获取href属性,我们必须进行List如下迭代:

find_href = browser.find_elements_by_xpath('//your_xpath')
for my_href in find_href:
    print(my_href.get_attribute("href"))

解决方案 2:

假设你有以下 HTML:

<div class="text-truncate trail-name">
<a href="http://google.com">Link 1</a>
</div>
<div class="text-truncate trail-name">
<a href="http://google.com">Link 2</a>
</div>
<div class="text-truncate trail-name">
<a href="http://google.com">Link 3</a>
</div>
<div class="text-truncate trail-name">
<a href="http://google.com">Link 4</a>
</div>

您的代码应如下所示:

all_trails = []

all_links = browser.find_elements_by_css_selector(".text-truncate.trail-name>a")

for link in all_links:

    all_trails.append(link.get_attribute("href"))

其中 all_trails 是链接列表(链接 1、链接 2 等等)。

希望它能帮到你!

解决方案 3:

find_href = browser.find_elements_by_xpath('//div[@class="text truncate trail-name"]/a[1]')
for i in find_href:
      all_trails.append(i.get_attribute('href'))

get_attribute作用于列表的元素,而不是列表本身。

解决方案 4:

请使用单数形式,而find_element_by_css_selector不是使用,find_elements_by_css_selector因为它会以列表形式返回多个 webElement。因此,您需要循环遍历每个 webElement 才能使用属性。

解决方案 5:

get_attribute 仅适用于该列表的元素,而不适用于列表本身。例如:

def fetch_img_urls(search_query: str):
    driver.get('https://images.google.com/')
    search = driver.find_element(By.CLASS_NAME, "gLFyf.gsfi")
    search.send_keys(search_query)
    search.send_keys(Keys.RETURN)
    links=[]
    try:
        time.sleep(5)
        urls = driver.find_elements(By.CSS_SELECTOR,'a.VFACy.kGQAp.sMi44c.lNHeqe.WGvvNb')
        for url in urls:
            #print(url.get_attribute("href"))
            links.append(url.get_attribute("href"))
            print(links)

    except Exception as e:
        print(f'error{e}')
        driver.quit()
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   3949  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   2735  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Freshdesk、ClickUp、nTask、Hubstaff、Plutio、Productive、Targa、Bonsai、Wrike。在当今快速变化的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多企业在项目管理过程中面临着诸多痛点,如任务分配不...
项目管理系统   74  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Monday、TeamGantt、Filestage、Chanty、Visor、Smartsheet、Productive、Quire、Planview。在当今快速变化的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多项目经理和团队在管理复杂项目时,常...
开源项目管理工具   82  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Smartsheet、GanttPRO、Backlog、Visor、ResourceGuru、Productive、Xebrio、Hive、Quire。在当今快节奏的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多企业在选择项目管理工具时常常面临困惑:...
项目管理系统   68  
热门文章
项目管理软件有哪些?
曾咪二维码

扫码咨询,免费领取项目管理大礼包!

云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用