import json from pydantic import BaseModel from browser_use import ( Agent, Controller, ) from lib.agents.run_agent import run_agent from lib.utils.logger import logger from lib.browser_use_utils.create_google_ai import create_google_ai from lib.config import GOOGLE_MODEL, GOOGLE_PLANNER_MODEL NOT_FOUND_LOGIN_PAGE = 0 FOUND_LOGIN_PAGE = 1 class FindLoginPageResponse(BaseModel): status: int = NOT_FOUND_LOGIN_PAGE # 0 if not found, 1 if found msg: str | None = None url: str | None = None async def find_login_page(target_url, session) -> tuple[bool, str | None]: initial_actions = [{"open_tab": {"url": target_url}}] task = """ You are an expert in finding login pages. Your task is to navigate to the login page of the given URL. Follow the steps below strictly and return results only in the specified format. ※ You are NOT allowed to navigate to URLs that are not directly discoverable within the initial domain. Do NOT use search engines or guess external login URLs. 0. INITIAL BLOCK CHECK - If the browser is blocked when trying to access the page — due to firewall, CAPTCHA, regional restrictions, or other access denials — immediately terminate the process and return the following JSON: ```json { "status": 0, "msg": "Blocked", "url": "" } ``` - Do NOT proceed to further steps in this case. 1. LOGIN PAGE NAVIGATION - Navigate only to a **client-side (non-enterprise)** login page within the provided domain. - Do NOT rely on external tools, search engines, or links not directly found on the site. - If a consent popup (e.g. for privacy/cookies) appears, you MUST dismiss or close it before proceeding. - Since step 0 confirmed access, assume the page now loads properly. 2. RETURN FORMAT - Once the login page is reached, return a JSON object matching the following schema: ```json { "status": 1, // 1 if login page is found, 0 otherwise "msg": "Login page found", // Optional message "url": "https://example.com/login" // Full URL of the login page if found } ``` - If the login page cannot be found, return: ```json { "status": 0, "msg": "Login page not found", "url": "" } ``` - Return ONLY the JSON object. Do NOT include any explanation, logging, or extra output. """ controller = Controller(output_model=FindLoginPageResponse, exclude_actions=['search_google']) agent = Agent( browser_session=session, initial_actions=initial_actions, task=task, llm=create_google_ai(GOOGLE_MODEL), controller=controller, ) is_failed, final_result = await run_agent(agent) if is_failed: logger(f"⚠️ 스캔 실패: {target_url} | {final_result}") print(f"⚠️ 스캔 실패: {target_url} | {final_result}") return False, None; data = json.loads(final_result) try: resp = FindLoginPageResponse(**data) if resp.status == FOUND_LOGIN_PAGE and len(resp.url) > 0: return True, resp.url else: return False, resp.msg except Exception as e: logger(f"⚠️ 결과 파싱 실패: {target_url} | {e}\n원본 결과: {data.msg}") print(f"⚠️ 결과 파싱 실패: {target_url} | {e}\n원본 결과: {data.msg}") return False, data.msg