@@ -44,6 +44,40 @@ def check_dependencies():
4444
4545 return True
4646
47+ async def wait_for_search_results (page , timeout = 5000 ):
48+ """等待搜索结果出现"""
49+ try :
50+ # 等待搜索结果容器有内容(不是"正在加载")
51+ await page .wait_for_function (
52+ """() => {
53+ const results = document.getElementById('search-results');
54+ if (!results) return false;
55+ const text = results.innerText || '';
56+ return text.length > 0 && !text.includes('正在加载');
57+ }""" ,
58+ timeout = timeout
59+ )
60+ # 额外等待一下确保渲染完成
61+ await asyncio .sleep (0.5 )
62+ return True
63+ except :
64+ return False
65+
66+ async def scroll_to_search_results (page ):
67+ """滚动到搜索结果区域,确保搜索结果在视口内"""
68+ try :
69+ # 获取搜索结果元素
70+ search_results = page .locator ('#search-results' )
71+ if await search_results .count () > 0 :
72+ # 滚动到搜索结果区域
73+ await search_results .scroll_into_view_if_needed ()
74+ await asyncio .sleep (0.3 )
75+ # 稍微向上滚动一点,让搜索框也可见
76+ await page .evaluate ("window.scrollBy(0, -100)" )
77+ await asyncio .sleep (0.2 )
78+ except Exception as e :
79+ print (f" ⚠️ 滚动到搜索结果时出错: { e } " )
80+
4781async def generate_gif ():
4882 """生成搜索功能演示 GIF"""
4983 try :
@@ -76,42 +110,64 @@ async def generate_gif():
76110
77111 # 步骤 2: 等待数据加载
78112 print ("⏳ 步骤 2: 等待数据加载..." )
113+ # 等待搜索框出现,说明页面已加载
114+ await page .wait_for_selector ('input[type="text"]' , timeout = 10000 )
79115 await asyncio .sleep (3 ) # 等待 all-books.json 加载
116+ # 检查数据是否加载完成
117+ data_loaded = await page .evaluate ("""() => {
118+ return window.books && window.books.length > 0;
119+ }""" )
120+ if data_loaded :
121+ print (f" ✅ 数据加载完成(已加载书籍数据)" )
122+ else :
123+ print (f" ⚠️ 数据可能还在加载中,继续..." )
80124 screenshots .append (await page .screenshot ())
81- print (" ✅ 数据加载完成" )
82125
83126 # 步骤 3: 输入搜索关键词 "文学"
84127 print ("🔍 步骤 3: 搜索 '文学'..." )
85128 search_input = page .locator ('input[type="text"]' )
86129 await search_input .fill ("文学" )
87- await asyncio .sleep (1.5 ) # 等待搜索结果
130+ await asyncio .sleep (0.5 ) # 等待输入完成
131+ # 等待搜索结果出现
132+ await wait_for_search_results (page , timeout = 5000 )
133+ # 滚动到搜索结果
134+ await scroll_to_search_results (page )
88135 screenshots .append (await page .screenshot ())
89- print (" ✅ 搜索完成" )
136+ print (" ✅ 搜索完成,已展示搜索结果 " )
90137
91- # 步骤 4: 清空并搜索 "历史 "
92- print ("🔍 步骤 4: 搜索 '历史 '..." )
138+ # 步骤 4: 清空并搜索 "文学 钢铁是怎么炼成的 "
139+ print ("🔍 步骤 4: 搜索 '文学 钢铁是怎么炼成的 '..." )
93140 await search_input .fill ("" )
94141 await asyncio .sleep (0.5 )
95- await search_input .fill ("历史" )
96- await asyncio .sleep (1.5 )
142+ await search_input .fill ("文学 钢铁是怎么炼成的" )
143+ await asyncio .sleep (0.5 )
144+ # 等待搜索结果
145+ await wait_for_search_results (page , timeout = 5000 )
146+ await scroll_to_search_results (page )
97147 screenshots .append (await page .screenshot ())
98- print (" ✅ 搜索完成" )
148+ print (" ✅ 搜索完成,已展示搜索结果 " )
99149
100150 # 步骤 5: 多关键词搜索
101- print ("🔍 步骤 5: 多关键词搜索 '沟通 励志 '..." )
151+ print ("🔍 步骤 5: 多关键词搜索 '沟通 樊登 职场 '..." )
102152 await search_input .fill ("" )
103153 await asyncio .sleep (0.5 )
104- await search_input .fill ("沟通 励志" )
105- await asyncio .sleep (1.5 )
154+ await search_input .fill ("沟通 樊登 职场" )
155+ await asyncio .sleep (0.5 )
156+ # 等待搜索结果
157+ await wait_for_search_results (page , timeout = 5000 )
158+ await scroll_to_search_results (page )
106159 screenshots .append (await page .screenshot ())
107- print (" ✅ 多关键词搜索完成" )
160+ print (" ✅ 多关键词搜索完成,已展示搜索结果 " )
108161
109- # 步骤 6: 展示最终结果
162+ # 步骤 6: 展示最终结果(多停留一会)
110163 await asyncio .sleep (1 )
164+ await scroll_to_search_results (page )
111165 screenshots .append (await page .screenshot ())
112166
113167 except Exception as e :
114168 print (f"❌ 录制过程中出错: { e } " )
169+ import traceback
170+ traceback .print_exc ()
115171 return False
116172 finally :
117173 await browser .close ()
@@ -124,6 +180,11 @@ async def generate_gif():
124180
125181 # 保存为 GIF
126182 OUTPUT_GIF .parent .mkdir (parents = True , exist_ok = True )
183+ # 第一帧显示时间稍长,其他帧正常显示
184+ durations = [2000 ] + [1500 ] * (len (images ) - 1 ) # 第一帧 2 秒,其他 1.5 秒
185+
186+ # 使用 save_all 保存多帧,但需要手动设置每帧的持续时间
187+ # 由于 PIL 的 save_all 不支持每帧不同 duration,我们使用固定值
127188 images [0 ].save (
128189 OUTPUT_GIF ,
129190 save_all = True ,
0 commit comments