nacho commited on
Commit
fc6d3fc
·
1 Parent(s): a750d84

fix: 重构模型开关点击逻辑,使用原生 JS 强制点击底层元素解决无头浏览器下点击失效的问题

Browse files
Files changed (1) hide show
  1. deepseek_browser.py +26 -24
deepseek_browser.py CHANGED
@@ -286,39 +286,41 @@ class DeepSeekBrowser:
286
 
287
  async def switch_model(self, model: str):
288
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
  # 极速思考模式
290
  if 'fast' in model or 'lite' in model:
291
- fast_btn = self.page.locator(
292
- 'text="极速思考", text="快速模式"'
293
- ).first
294
- if await fast_btn.count() > 0:
295
- await fast_btn.click()
296
- await asyncio.sleep(0.5)
297
  # 深度思考 (DeepThink R1)
298
  elif 'reasoner' in model or 'thinking' in model or 'pro' in model:
299
- thinking_btn = self.page.locator(
300
- 'text="深度思考", text="DeepThink", text="R1"'
301
- ).first
302
- if await thinking_btn.count() > 0:
303
- await thinking_btn.click()
304
- await asyncio.sleep(0.5)
305
 
306
  # 专家模式 (独立开关)
307
  if 'expert' in model:
308
- expert_btn = self.page.locator(
309
- 'text="专家模式"'
310
- ).first
311
- if await expert_btn.count() > 0:
312
- await expert_btn.click()
313
- await asyncio.sleep(0.5)
314
 
315
  if 'search' in model:
316
- search_btn = self.page.locator('text="智能搜索"').first
317
- if await search_btn.count() > 0:
318
- await search_btn.click()
319
- await asyncio.sleep(0.5)
320
- except Exception:
321
- pass
322
 
323
  async def send_message(self, prompt: str, timeout: int = 120, model: str = "deepseek-chat") -> dict:
324
  """Send message and return {'content': str, 'reasoning_content': str}."""
 
286
 
287
  async def switch_model(self, model: str):
288
  try:
289
+ # Inject a robust clicker that finds the innermost element containing specific text
290
+ # and clicks it directly via JS, bypassing Playwright's actionability checks.
291
+ click_js = """(texts) => {
292
+ const els = Array.from(document.querySelectorAll('*'));
293
+ const target = els.reverse().find(el => {
294
+ if (!el.innerText || el.children.length > 0) return false;
295
+ return texts.some(t => el.innerText.includes(t)) && el.offsetParent !== null;
296
+ });
297
+ if (target) {
298
+ target.click();
299
+ return true;
300
+ }
301
+ return false;
302
+ }"""
303
+
304
  # 极速思考模式
305
  if 'fast' in model or 'lite' in model:
306
+ await self.page.evaluate(click_js, ['极速思考', '快速模式'])
307
+ await asyncio.sleep(0.5)
308
+
 
 
 
309
  # 深度思考 (DeepThink R1)
310
  elif 'reasoner' in model or 'thinking' in model or 'pro' in model:
311
+ await self.page.evaluate(click_js, ['深度思考', 'DeepThink', 'R1'])
312
+ await asyncio.sleep(0.5)
 
 
 
 
313
 
314
  # 专家模式 (独立开关)
315
  if 'expert' in model:
316
+ await self.page.evaluate(click_js, ['专家模式'])
317
+ await asyncio.sleep(0.5)
 
 
 
 
318
 
319
  if 'search' in model:
320
+ await self.page.evaluate(click_js, ['智能搜索'])
321
+ await asyncio.sleep(0.5)
322
+ except Exception as e:
323
+ logger.warning("[switch_model] click error: %s", e)
 
 
324
 
325
  async def send_message(self, prompt: str, timeout: int = 120, model: str = "deepseek-chat") -> dict:
326
  """Send message and return {'content': str, 'reasoning_content': str}."""