You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python/Selenium无法遍历搜索结果页标题并打印至控制台

问题:Selenium无法遍历打印搜索结果中的酱料名称

问题场景

使用Python Selenium操作测试网站https://www.ionme.dev,目标是通过for循环将搜索结果页上所有类名为sauce-name的酱料名称打印到控制台。前4步操作(打开Chrome网站、点击右上角汉堡菜单、搜索“ghost”、显示搜索结果)均正常,但第5步遍历打印无输出。目标元素嵌套在多个类名为card的卡片中。

原代码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

service = Service()
driver = webdriver.Chrome(service=service, options=chrome_options)

driver.get("http://ionme.dev")

dropdown = driver.find_element(By.CLASS_NAME, "navbar-toggler-icon")
dropdown.click()

search = driver.find_element("name", "secret_sauce_search_query")
search.send_keys("ghost")
search.send_keys(Keys.RETURN)

try:
    main = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "title"))
    )

    titles = main.find_elements(By.CLASS_NAME, "card")
    for title in titles:
        header = title.find_element(By.CLASS_NAME, "sauce-name")
        print(header.text)

except:
    driver.close()

问题分析

原代码核心问题在于等待的元素错误:

  • 搜索结果的card卡片实际嵌套在类名为search-card-section的容器中,而非id="title"的元素下。通过main = WebDriverWait(...)获取的title元素并不包含目标卡片,导致后续titles = main.find_elements(...)得到空列表,自然无输出。

修正方案

方案1:直接定位所有sauce-name元素

跳过中间卡片容器,直接等待并定位所有目标元素:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

service = Service()
driver = webdriver.Chrome(service=service, options=chrome_options)

driver.get("http://ionme.dev")

# 打开汉堡菜单
dropdown = driver.find_element(By.CLASS_NAME, "navbar-toggler-icon")
dropdown.click()

# 执行搜索
search = driver.find_element(By.NAME, "secret_sauce_search_query")
search.send_keys("ghost")
search.send_keys(Keys.RETURN)

try:
    # 等待所有sauce-name元素加载完成
    sauce_names = WebDriverWait(driver, 10).until(
        EC.presence_of_all_elements_located((By.CLASS_NAME, "sauce-name"))
    )
    
    # 遍历打印每个酱料名称
    for name in sauce_names:
        print(name.text)

except Exception as e:
    print(f"错误信息: {e}")
    driver.close()

方案2:先定位正确的容器再遍历卡片

先找到包含所有卡片的search-card-section容器,再遍历其中的card元素:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

service = Service()
driver = webdriver.Chrome(service=service, options=chrome_options)

driver.get("http://ionme.dev")

dropdown = driver.find_element(By.CLASS_NAME, "navbar-toggler-icon")
dropdown.click()

search = driver.find_element(By.NAME, "secret_sauce_search_query")
search.send_keys("ghost")
search.send_keys(Keys.RETURN)

try:
    # 等待搜索结果容器加载
    result_container = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CLASS_NAME, "search-card-section"))
    )
    
    # 获取所有卡片元素
    cards = result_container.find_elements(By.CLASS_NAME, "card")
    
    # 遍历卡片提取酱料名称
    for card in cards:
        sauce_name = card.find_element(By.CLASS_NAME, "sauce-name")
        print(sauce_name.text)

except Exception as e:
    print(f"错误信息: {e}")
    driver.close()

内容的提问来源于stack exchange,提问作者Younion Miller

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 19:09:58