Compare commits
2
Commits
8eccfa1833
...
7625b3a9ad
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7625b3a9ad | ||
|
|
c706116de3 |
@@ -963,29 +963,67 @@ async def run_browser_task(task_id: str, task: str, max_steps: int, model: str):
|
||||
if custom_tools is not None:
|
||||
agent_kwargs["tools"] = custom_tools
|
||||
|
||||
# Добавляем информацию о капче в промпт, если включено авто-решение
|
||||
if AUTO_SOLVE_CAPTCHA and CAPSOLVER_API_KEY:
|
||||
captcha_hint = "\n\nIMPORTANT: If you encounter a CAPTCHA (reCAPTCHA, hCaptcha, or Cloudflare Turnstile), use the solve_captcha tool to automatically solve it. The tool requires CAPSOLVER_API_KEY to be configured."
|
||||
agent_kwargs["task"] = task + captcha_hint
|
||||
|
||||
agent = Agent(**agent_kwargs)
|
||||
|
||||
# Автоматическое решение капчи перед запуском, если включено
|
||||
if AUTO_SOLVE_CAPTCHA and CAPSOLVER_API_KEY:
|
||||
logger.info(f"[{task_id}] AUTO_SOLVE_CAPTCHA enabled, checking for captcha...")
|
||||
try:
|
||||
# Получаем страницу через browser_use
|
||||
from browser_use import BrowserSession
|
||||
browser_session = BrowserSession(browser=browser)
|
||||
page = await browser_session.get_current_page()
|
||||
# Получаем страницу через browser
|
||||
page = None
|
||||
try:
|
||||
# Пробуем получить страницу через CDP
|
||||
import urllib.request
|
||||
import json
|
||||
response = urllib.request.urlopen(f"{cdp_url}/json/list", timeout=5)
|
||||
targets = json.loads(response.read().decode())
|
||||
for target in targets:
|
||||
if target.get('type') == 'page' and target.get('url', '') not in ['about:blank', 'chrome://newtab/']:
|
||||
# Найдена страница с URL
|
||||
logger.info(f"[{task_id}] Found page: {target.get('url')}")
|
||||
# Подключаемся к странице через Playwright
|
||||
from playwright.async_api import async_playwright
|
||||
async with async_playwright() as p:
|
||||
browser = await p.chromium.connect_over_cdp(cdp_url)
|
||||
context = browser.contexts[0] if browser.contexts else None
|
||||
if context:
|
||||
pages = context.pages
|
||||
if pages:
|
||||
page = pages[0]
|
||||
break
|
||||
except Exception as e_page:
|
||||
logger.warning(f"[{task_id}] Could not get page: {e_page}")
|
||||
|
||||
if page:
|
||||
logger.info(f"[{task_id}] Page obtained, checking for captcha elements...")
|
||||
# Проверяем наличие капчи
|
||||
captcha_found = await page.query_selector('[data-sitekey]')
|
||||
turnstile_found = await page.query_selector('iframe[src*="turnstile"]')
|
||||
if captcha_found or turnstile_found:
|
||||
logger.info(f"[{task_id}] CAPTCHA detected, attempting to solve automatically...")
|
||||
# Вызываем функцию решения капчи
|
||||
solved = await solve_captcha_with_capsolver(page, CAPSOLVER_API_KEY)
|
||||
if solved:
|
||||
logger.info(f"[{task_id}] CAPTCHA solved successfully!")
|
||||
try:
|
||||
captcha_found = await page.query_selector('[data-sitekey]')
|
||||
turnstile_found = await page.query_selector('iframe[src*="turnstile"]')
|
||||
logger.info(f"[{task_id}] Captcha found: {captcha_found}, Turnstile found: {turnstile_found}")
|
||||
if captcha_found or turnstile_found:
|
||||
logger.info(f"[{task_id}] CAPTCHA detected, attempting to solve automatically...")
|
||||
# Вызываем функцию решения капчи
|
||||
solved = await solve_captcha_with_capsolver(page, CAPSOLVER_API_KEY)
|
||||
if solved:
|
||||
logger.info(f"[{task_id}] CAPTCHA solved successfully!")
|
||||
else:
|
||||
logger.warning(f"[{task_id}] Failed to solve CAPTCHA automatically")
|
||||
else:
|
||||
logger.warning(f"[{task_id}] Failed to solve CAPTCHA automatically")
|
||||
logger.info(f"[{task_id}] No captcha detected on page")
|
||||
except Exception as e_check:
|
||||
logger.warning(f"[{task_id}] Error checking captcha: {e_check}")
|
||||
else:
|
||||
logger.warning(f"[{task_id}] Could not get page from browser")
|
||||
except Exception as e:
|
||||
logger.warning(f"[{task_id}] Auto-captcha solve failed: {e}")
|
||||
logger.error(f"[{task_id}] Auto-captcha solve failed: {e}")
|
||||
else:
|
||||
logger.info(f"[{task_id}] AUTO_SOLVE_CAPTCHA={AUTO_SOLVE_CAPTCHA}, CAPSOLVER_API_KEY={'set' if CAPSOLVER_API_KEY else 'not set'}")
|
||||
|
||||
# Выполнение задачи
|
||||
history: AgentHistoryList = await agent.run(max_steps=max_steps)
|
||||
|
||||
Reference in new issue
Block a user