StarrySkyWorld commited on
Commit
c33aa8a
·
verified ·
1 Parent(s): 5c5aab7

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +78 -35
main.py CHANGED
@@ -75,27 +75,44 @@ async def activate_task():
75
  print(f"[Activate] Error: {e}")
76
 
77
  async def auto_click_task():
78
- """自动点击 'Got it' 按钮"""
79
- global page
80
  if not page:
81
  return
82
 
83
- # 匹配多种大小写形式
84
- text_patterns = ["Got it", "GOT IT", "Got It", "got it", "GOTIT", "Gotit"]
85
 
86
  try:
87
- # 1. 先在主页面查找
88
- for text in text_patterns:
89
  selectors = [
 
90
  f'button:has-text("{text}")',
91
  f'a:has-text("{text}")',
92
  f'[role="button"]:has-text("{text}")',
93
- f'div:has-text("{text}"):not(:has(div:has-text("{text}")))', # 最内层div
94
- f'span:has-text("{text}")',
95
- f'mat-button:has-text("{text}")', # Angular Material
96
- f'[data-mdc-dialog-action]:has-text("{text}")', # Material Dialog
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  ]
98
 
 
99
  for selector in selectors:
100
  try:
101
  element = page.locator(selector).first
@@ -105,23 +122,12 @@ async def auto_click_task():
105
  return
106
  except:
107
  continue
108
-
109
- # 2. 在所有 iframe 中查找
110
- frames = page.frames
111
- for frame in frames:
112
- if frame == page.main_frame:
113
- continue
114
 
115
- for text in text_patterns:
116
- selectors = [
117
- f'button:has-text("{text}")',
118
- f'a:has-text("{text}")',
119
- f'[role="button"]:has-text("{text}")',
120
- f'div:has-text("{text}"):not(:has(div:has-text("{text}")))',
121
- f'span:has-text("{text}")',
122
- ]
123
-
124
- for selector in selectors:
125
  try:
126
  element = frame.locator(selector).first
127
  if await element.is_visible(timeout=100):
@@ -205,17 +211,18 @@ async def lifespan(app: FastAPI):
205
  )
206
  print(f"[Scheduler] Activate task started with interval: {interval}s")
207
 
208
- # 自动点击 "Got it" 按钮任务
 
209
  scheduler.add_job(
210
  auto_click_task,
211
  "interval",
212
- seconds=5,
213
  max_instances=1,
214
  coalesce=True,
215
  misfire_grace_time=10,
216
  replace_existing=True
217
  )
218
- print(f"[Scheduler] Auto-click task started with interval: 5s")
219
 
220
  scheduler.start()
221
 
@@ -290,15 +297,17 @@ async def manual_click(text: str):
290
  f'button:has-text("{text}")',
291
  f'a:has-text("{text}")',
292
  f'[role="button"]:has-text("{text}")',
293
- f'div:has-text("{text}")',
294
- f'span:has-text("{text}")',
 
 
295
  ]
296
 
297
  # 主页面
298
  for selector in selectors:
299
  try:
300
  element = page.locator(selector).first
301
- if await element.is_visible(timeout=500):
302
  await element.click(timeout=3000)
303
  return {"status": "success", "message": f'Clicked "{text}" in main page'}
304
  except:
@@ -308,7 +317,7 @@ async def manual_click(text: str):
308
  for frame in page.frames:
309
  if frame == page.main_frame:
310
  continue
311
- for selector in selectors:
312
  try:
313
  element = frame.locator(selector).first
314
  if await element.is_visible(timeout=500):
@@ -333,7 +342,7 @@ async def debug_buttons():
333
 
334
  # 主页面按钮
335
  button_elements = await page.locator('button, [role="button"], a[class*="button"], a[class*="btn"]').all()
336
- for i, btn in enumerate(button_elements[:20]): # 最多20个
337
  try:
338
  text = await btn.inner_text(timeout=500)
339
  visible = await btn.is_visible(timeout=500)
@@ -346,6 +355,24 @@ async def debug_buttons():
346
  except:
347
  continue
348
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
349
  # iframe 中的按钮
350
  for frame_idx, frame in enumerate(page.frames):
351
  if frame == page.main_frame:
@@ -380,11 +407,27 @@ async def debug_html(selector: str = "body"):
380
 
381
  try:
382
  html = await page.locator(selector).first.inner_html(timeout=5000)
383
- # 截取部分
384
  return {"status": "success", "html": html[:5000]}
385
  except Exception as e:
386
  return {"status": "error", "message": str(e)}
387
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
388
  @app.get("/health")
389
  async def health_check():
390
  """健康检查"""
 
75
  print(f"[Activate] Error: {e}")
76
 
77
  async def auto_click_task():
78
+ """自动点击指定按钮"""
79
+ global page, config
80
  if not page:
81
  return
82
 
83
+ # 从配置获取要点击的按钮文本列表
84
+ click_buttons = config.get("autoClickButtons", ["Got it", "GOT IT", "Accept", "I agree", "OK", "Okay", "Dismiss", "Close"])
85
 
86
  try:
87
+ for text in click_buttons:
88
+ # 各种选择器
89
  selectors = [
90
+ # 标准按钮
91
  f'button:has-text("{text}")',
92
  f'a:has-text("{text}")',
93
  f'[role="button"]:has-text("{text}")',
94
+ # Material/Angular 组件
95
+ f'mat-button:has-text("{text}")',
96
+ f'mat-dialog-actions button:has-text("{text}")',
97
+ f'.mat-mdc-dialog-actions button:has-text("{text}")',
98
+ f'.cdk-overlay-container button:has-text("{text}")',
99
+ f'.mdc-dialog button:has-text("{text}")',
100
+ f'.mdc-dialog__actions button:has-text("{text}")',
101
+ # 通用弹窗
102
+ f'[class*="dialog"] button:has-text("{text}")',
103
+ f'[class*="modal"] button:has-text("{text}")',
104
+ f'[class*="popup"] button:has-text("{text}")',
105
+ f'[class*="overlay"] button:has-text("{text}")',
106
+ f'[class*="toast"] button:has-text("{text}")',
107
+ f'[class*="snack"] button:has-text("{text}")',
108
+ f'[class*="banner"] button:has-text("{text}")',
109
+ f'[class*="consent"] button:has-text("{text}")',
110
+ # 文本匹配
111
+ f'span:text-is("{text}")',
112
+ f'div:text-is("{text}")',
113
  ]
114
 
115
+ # 主页面查找
116
  for selector in selectors:
117
  try:
118
  element = page.locator(selector).first
 
122
  return
123
  except:
124
  continue
 
 
 
 
 
 
125
 
126
+ # 所有 iframe 中查找
127
+ for frame in page.frames:
128
+ if frame == page.main_frame:
129
+ continue
130
+ for selector in selectors[:5]: # iframe中只用基础选择器
 
 
 
 
 
131
  try:
132
  element = frame.locator(selector).first
133
  if await element.is_visible(timeout=100):
 
211
  )
212
  print(f"[Scheduler] Activate task started with interval: {interval}s")
213
 
214
+ # 自动点击按钮任务
215
+ click_interval = config.get("autoClickInterval", 5)
216
  scheduler.add_job(
217
  auto_click_task,
218
  "interval",
219
+ seconds=click_interval,
220
  max_instances=1,
221
  coalesce=True,
222
  misfire_grace_time=10,
223
  replace_existing=True
224
  )
225
+ print(f"[Scheduler] Auto-click task started with interval: {click_interval}s")
226
 
227
  scheduler.start()
228
 
 
297
  f'button:has-text("{text}")',
298
  f'a:has-text("{text}")',
299
  f'[role="button"]:has-text("{text}")',
300
+ f'.cdk-overlay-container button:has-text("{text}")',
301
+ f'[class*="dialog"] button:has-text("{text}")',
302
+ f'div:text-is("{text}")',
303
+ f'span:text-is("{text}")',
304
  ]
305
 
306
  # 主页面
307
  for selector in selectors:
308
  try:
309
  element = page.locator(selector).first
310
+ if await element.is_visible(timeout=1000):
311
  await element.click(timeout=3000)
312
  return {"status": "success", "message": f'Clicked "{text}" in main page'}
313
  except:
 
317
  for frame in page.frames:
318
  if frame == page.main_frame:
319
  continue
320
+ for selector in selectors[:3]:
321
  try:
322
  element = frame.locator(selector).first
323
  if await element.is_visible(timeout=500):
 
342
 
343
  # 主页面按钮
344
  button_elements = await page.locator('button, [role="button"], a[class*="button"], a[class*="btn"]').all()
345
+ for i, btn in enumerate(button_elements[:30]):
346
  try:
347
  text = await btn.inner_text(timeout=500)
348
  visible = await btn.is_visible(timeout=500)
 
355
  except:
356
  continue
357
 
358
+ # CDK Overlay (Angular Material 弹窗)
359
+ try:
360
+ overlay_buttons = await page.locator('.cdk-overlay-container button, .cdk-overlay-container [role="button"]').all()
361
+ for i, btn in enumerate(overlay_buttons[:10]):
362
+ try:
363
+ text = await btn.inner_text(timeout=500)
364
+ visible = await btn.is_visible(timeout=500)
365
+ buttons.append({
366
+ "index": i,
367
+ "text": text.strip()[:50],
368
+ "visible": visible,
369
+ "frame": "cdk-overlay"
370
+ })
371
+ except:
372
+ continue
373
+ except:
374
+ pass
375
+
376
  # iframe 中的按钮
377
  for frame_idx, frame in enumerate(page.frames):
378
  if frame == page.main_frame:
 
407
 
408
  try:
409
  html = await page.locator(selector).first.inner_html(timeout=5000)
 
410
  return {"status": "success", "html": html[:5000]}
411
  except Exception as e:
412
  return {"status": "error", "message": str(e)}
413
 
414
+ @app.get("/debug/overlay")
415
+ async def debug_overlay():
416
+ """调试:检查 CDK Overlay 弹窗内容"""
417
+ global page
418
+ if not page:
419
+ return {"status": "error", "message": "Browser not initialized"}
420
+
421
+ try:
422
+ overlay = page.locator('.cdk-overlay-container')
423
+ if await overlay.count() > 0:
424
+ html = await overlay.inner_html(timeout=5000)
425
+ return {"status": "success", "html": html[:5000]}
426
+ else:
427
+ return {"status": "success", "html": "No overlay found"}
428
+ except Exception as e:
429
+ return {"status": "error", "message": str(e)}
430
+
431
  @app.get("/health")
432
  async def health_check():
433
  """健康检查"""