Spaces:
Paused
Paused
fix(browser): make URL validation robust to redirects
Browse filesFiles changed (2)
- browser/instance.py+12-4
- utils/url_helper.pyAdded+46
5 unmodified lines67891011132 unmodified lines14414514614714814915015115244 unmodified lines1971981992002012022035 unmodified linesfrom camoufox.sync_api import Camoufoxfrom utils.paths import logs_dirfrom utils.common import parse_headless_mode, ensure_dirdef run_browser_instance(config):132 unmodified lineslogger.error("检测到Google登录页面(需要输入邮箱)。Cookie已完全失效。")page.screenshot(path=os.path.join(screenshot_dir, f"FAIL_identifier_page_{diagnostic_tag}.png"))returnelif expected_url.split('?')[0] in final_url:logger.info("URL正确。现在等待页面完成初始加载...")# --- NEW ROBUST STRATEGY: Wait for the loading spinner to disappear ---# This is the key to solving the race condition. The error message or44 unmodified linespage.screenshot(path=os.path.join(screenshot_dir, f"FAIL_chooser_click_failed_{diagnostic_tag}.png"))returnelse:logger.error(f"导航到了一个意外的URL: {final_url}")page.screenshot(path=os.path.join(screenshot_dir, f"FAIL_unexpected_url_{diagnostic_tag}.png"))return5 unmodified lines6789101112132 unmodified lines14514614714814915015115215315415515615744 unmodified lines2022032042052062072082092102115 unmodified linesfrom camoufox.sync_api import Camoufoxfrom utils.paths import logs_dirfrom utils.common import parse_headless_mode, ensure_dirfrom utils.url_helper import extract_url_pathdef run_browser_instance(config):132 unmodified lineslogger.error("检测到Google登录页面(需要输入邮箱)。Cookie已完全失效。")page.screenshot(path=os.path.join(screenshot_dir, f"FAIL_identifier_page_{diagnostic_tag}.png"))return# 提取路径部分进行匹配(允许域名重定向)expected_path = extract_url_path(expected_url).split('?')[0]final_path = extract_url_path(final_url)if expected_path and expected_path in final_path:logger.info(f"URL验证通过。预期路径: {expected_path}, 最终URL: {final_url}")# --- NEW ROBUST STRATEGY: Wait for the loading spinner to disappear ---# This is the key to solving the race condition. The error message or44 unmodified linespage.screenshot(path=os.path.join(screenshot_dir, f"FAIL_chooser_click_failed_{diagnostic_tag}.png"))returnelse:logger.error(f"导航到了意外的URL。")logger.error(f" 预期路径: {expected_path}")logger.error(f" 最终URL: {final_url}")logger.error(f" 最终路径: {final_path}")page.screenshot(path=os.path.join(screenshot_dir, f"FAIL_unexpected_url_{diagnostic_tag}.png"))return
12345678910111213141516171819202122232425262728293031323334353637383940414243444546"""URL处理辅助函数
提供URL解析和路径提取功能,用于导航验证中的域名无关匹配。"""
from urllib.parse import urlparse
def extract_url_path(url: str) -> str: """ 提取URL的路径和查询参数部分,忽略协议和域名差异
用于验证导航是否到达正确页面,允许域名重定向。
Args: url: 完整URL字符串
Returns: 路径+查询参数+片段(例如:"/apps/drive/123?param=value#section") 如果URL为空或无效,返回空字符串
Examples: >>> extract_url_path("https://ai.studio/apps/drive/123?param=value") '/apps/drive/123?param=value'
>>> extract_url_path("https://aistudio.google.com/apps/drive/123") '/apps/drive/123'
>>> extract_url_path("https://example.com/path") '/path' """ if not url: return ""
try: parsed = urlparse(url) result = parsed.path if parsed.query: result += '?' + parsed.query if parsed.fragment: result += '#' + parsed.fragment return result except Exception: # 如果URL格式无效,返回空字符串 return ""browser/instance.py+12-4
utils/url_helper.pyAdded+46