feat(val-blog): add 2026-04-30 dream journey post
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
测试脚本:使用代理访问 Google
|
||||
"""
|
||||
|
||||
import sys
|
||||
sys.path.insert(0, '/Users/guchen/.openclaw/workspace/skills/browser-automation')
|
||||
|
||||
from playwright.sync_api import sync_playwright
|
||||
import os
|
||||
|
||||
# 代理配置
|
||||
PROXY = {"server": "http://127.0.0.1:7897"}
|
||||
|
||||
def test_google_with_proxy():
|
||||
"""测试使用代理访问 Google"""
|
||||
|
||||
print("=" * 60)
|
||||
print("开始测试:使用代理访问 Google")
|
||||
print(f"代理配置: {PROXY}")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
with sync_playwright() as p:
|
||||
# 启动浏览器
|
||||
print("\n[1/5] 启动 Chrome 浏览器...")
|
||||
browser = p.chromium.launch(
|
||||
executable_path="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
|
||||
headless=False, # 设置为 True 可以无头运行
|
||||
args=[
|
||||
"--disable-blink-features=AutomationControlled",
|
||||
"--disable-web-security",
|
||||
"--window-size=1280,720",
|
||||
]
|
||||
)
|
||||
|
||||
# 创建带有代理的上下文
|
||||
print("[2/5] 创建带有代理的浏览器上下文...")
|
||||
context = browser.new_context(
|
||||
viewport={"width": 1280, "height": 720},
|
||||
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
||||
proxy=PROXY # 关键:传入代理配置
|
||||
)
|
||||
|
||||
# 注入反检测脚本
|
||||
context.add_init_script("""
|
||||
Object.defineProperty(navigator, 'webdriver', { get: () => undefined });
|
||||
window.chrome = { runtime: {} };
|
||||
""")
|
||||
|
||||
# 创建新页面
|
||||
page = context.new_page()
|
||||
|
||||
# 访问 Google
|
||||
print("[3/5] 访问 https://www.google.com ...")
|
||||
page.goto("https://www.google.com", wait_until="networkidle")
|
||||
page.wait_for_timeout(2000) # 等待页面完全加载
|
||||
|
||||
# 获取页面信息
|
||||
print("[4/5] 获取页面信息...")
|
||||
title = page.title()
|
||||
url = page.url
|
||||
|
||||
# 获取页面文本内容(前500字符)
|
||||
text_content = page.inner_text("body")
|
||||
text_preview = text_content[:500].replace("\n", " ")
|
||||
|
||||
# 获取可交互元素
|
||||
elements = page.query_selector_all('a, button, input, textarea, select')
|
||||
element_count = len(elements)
|
||||
|
||||
# 截图
|
||||
print("[5/5] 保存页面截图...")
|
||||
screenshot_path = "/tmp/google_test.png"
|
||||
page.screenshot(path=screenshot_path, full_page=True)
|
||||
|
||||
# 获取截图文件大小
|
||||
screenshot_size = os.path.getsize(screenshot_path)
|
||||
|
||||
# 关闭浏览器
|
||||
browser.close()
|
||||
|
||||
# 输出结果
|
||||
print("\n" + "=" * 60)
|
||||
print("测试结果: ✅ 成功")
|
||||
print("=" * 60)
|
||||
print(f"页面标题: {title}")
|
||||
print(f"页面URL: {url}")
|
||||
print(f"交互元素数量: {element_count}")
|
||||
print(f"截图文件: {screenshot_path}")
|
||||
print(f"截图大小: {screenshot_size / 1024:.2f} KB")
|
||||
print("\n页面内容摘要 (前500字符):")
|
||||
print("-" * 60)
|
||||
print(text_preview)
|
||||
print("-" * 60)
|
||||
|
||||
# 验证关键元素
|
||||
has_google = "Google" in text_content or "google" in text_content.lower()
|
||||
has_search = "search" in text_content.lower() or "搜索" in text_content
|
||||
|
||||
print("\n验证结果:")
|
||||
print(f" - 包含 'Google' 文本: {'✅' if has_google else '❌'}")
|
||||
print(f" - 包含搜索相关元素: {'✅' if has_search else '❌'}")
|
||||
|
||||
if has_google and screenshot_size > 1000:
|
||||
print("\n✅ 测试通过:代理配置工作正常!")
|
||||
return True
|
||||
else:
|
||||
print("\n⚠️ 测试部分通过:页面加载但可能不完全")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print("\n" + "=" * 60)
|
||||
print("测试结果: ❌ 失败")
|
||||
print("=" * 60)
|
||||
print(f"错误类型: {type(e).__name__}")
|
||||
print(f"错误信息: {str(e)}")
|
||||
print("\n详细堆栈:")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = test_google_with_proxy()
|
||||
sys.exit(0 if success else 1)
|
||||
Reference in New Issue
Block a user