遍历 WebElements 时,“list”对象没有属性“get_attribute”
- 2025-04-15 09:19:00
- admin 原创
- 31
问题描述:
我正在尝试使用 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
似乎以下行返回两个WebElements
,List
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()
扫码咨询,免费领取项目管理大礼包!