commit
This commit is contained in:
170
backend/test_basic_browser.py
Normal file
170
backend/test_basic_browser.py
Normal file
@@ -0,0 +1,170 @@
|
||||
"""
|
||||
基础浏览器测试脚本
|
||||
用于测试浏览器是否能正常加载小红书页面
|
||||
"""
|
||||
import asyncio
|
||||
from playwright.async_api import async_playwright
|
||||
import sys
|
||||
|
||||
|
||||
async def test_basic_browser(proxy_index: int = 0):
|
||||
"""基础浏览器测试"""
|
||||
print(f"\n{'='*60}")
|
||||
print(f"🔍 基础浏览器测试")
|
||||
print(f"{'='*60}")
|
||||
|
||||
# 从代理配置获取代理信息
|
||||
from damai_proxy_config import get_proxy_config
|
||||
proxy_config = get_proxy_config(proxy_index)
|
||||
proxy_server = proxy_config['server'].replace('http://', '')
|
||||
proxy_url = f"http://{proxy_config['username']}:{proxy_config['password']}@{proxy_server}"
|
||||
|
||||
print(f"✅ 使用代理: 代理{proxy_index + 1}")
|
||||
print(f" 代理服务器: {proxy_config['server']}")
|
||||
|
||||
try:
|
||||
async with async_playwright() as p:
|
||||
# 配置代理
|
||||
proxy_parts = proxy_url.replace('http://', '').replace('https://', '').split('@')
|
||||
if len(proxy_parts) == 2:
|
||||
auth_part = proxy_parts[0]
|
||||
server_part = proxy_parts[1]
|
||||
username, password = auth_part.split(':')
|
||||
|
||||
proxy_config_obj = {
|
||||
"server": f"http://{server_part}",
|
||||
"username": username,
|
||||
"password": password
|
||||
}
|
||||
else:
|
||||
proxy_config_obj = {"server": proxy_url}
|
||||
|
||||
print(f" 配置的代理对象: {proxy_config_obj}")
|
||||
|
||||
# 启动浏览器
|
||||
browser = await p.chromium.launch(
|
||||
headless=False, # 非无头模式,便于观察
|
||||
proxy=proxy_config_obj
|
||||
)
|
||||
|
||||
# 创建上下文
|
||||
context = await browser.new_context(
|
||||
user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
||||
)
|
||||
|
||||
# 创建页面
|
||||
page = await context.new_page()
|
||||
|
||||
print(f"\n🌐 尝试访问百度...")
|
||||
try:
|
||||
await page.goto('https://www.baidu.com', wait_until='networkidle', timeout=15000)
|
||||
await asyncio.sleep(2)
|
||||
|
||||
title = await page.title()
|
||||
url = page.url
|
||||
content_len = len(await page.content())
|
||||
|
||||
print(f" ✅ 百度访问成功")
|
||||
print(f" 标题: {title}")
|
||||
print(f" URL: {url}")
|
||||
print(f" 内容长度: {content_len} 字符")
|
||||
except Exception as e:
|
||||
print(f" ❌ 百度访问失败: {str(e)}")
|
||||
|
||||
print(f"\n🌐 尝试访问小红书登录页...")
|
||||
try:
|
||||
await page.goto('https://creator.xiaohongshu.com/login', wait_until='networkidle', timeout=15000)
|
||||
await asyncio.sleep(5) # 等待更长时间
|
||||
|
||||
title = await page.title()
|
||||
url = page.url
|
||||
content = await page.content()
|
||||
content_len = len(content)
|
||||
|
||||
print(f" 访问结果:")
|
||||
print(f" 标题: {title}")
|
||||
print(f" URL: {url}")
|
||||
print(f" 内容长度: {content_len} 字符")
|
||||
|
||||
# 检查是否有特定内容
|
||||
if content_len == 0:
|
||||
print(f" ⚠️ 页面内容为空,可能存在加载问题")
|
||||
elif "验证" in content or "captcha" in content.lower() or "安全" in content:
|
||||
print(f" ⚠️ 检测到验证或安全提示")
|
||||
else:
|
||||
print(f" ✅ 页面加载正常")
|
||||
|
||||
# 查找页面上的所有元素
|
||||
print(f"\n🔍 分析页面元素...")
|
||||
|
||||
# 查找所有input元素
|
||||
inputs = await page.query_selector_all('input')
|
||||
print(f" 找到 {len(inputs)} 个input元素")
|
||||
|
||||
# 查找所有表单相关元素
|
||||
form_elements = await page.query_selector_all('input, button, select, textarea')
|
||||
print(f" 找到 {len(form_elements)} 个表单相关元素")
|
||||
|
||||
# 打印前几个元素的信息
|
||||
for i, elem in enumerate(form_elements[:5]):
|
||||
try:
|
||||
tag = await elem.evaluate('el => el.tagName')
|
||||
text = await elem.inner_text()
|
||||
placeholder = await elem.get_attribute('placeholder')
|
||||
class_name = await elem.get_attribute('class')
|
||||
id_attr = await elem.get_attribute('id')
|
||||
|
||||
print(f" 元素 {i+1}:")
|
||||
print(f" - 标签: {tag}")
|
||||
print(f" - 文本: {text[:50]}...")
|
||||
print(f" - placeholder: {placeholder}")
|
||||
print(f" - class: {class_name[:50]}...")
|
||||
print(f" - id: {id_attr}")
|
||||
except Exception as e:
|
||||
print(f" 元素 {i+1}: 获取信息失败 - {str(e)}")
|
||||
|
||||
except Exception as e:
|
||||
print(f" ❌ 小红书访问失败: {str(e)}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
print(f"\n⏸️ 浏览器保持打开状态,您可以手动检查页面")
|
||||
print(f" 按 Enter 键关闭浏览器...")
|
||||
|
||||
# 等待用户输入
|
||||
input()
|
||||
|
||||
await browser.close()
|
||||
print(f"✅ 浏览器已关闭")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 测试过程异常: {str(e)}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
async def main():
|
||||
"""主函数"""
|
||||
print("="*60)
|
||||
print("🔍 基础浏览器测试工具")
|
||||
print("="*60)
|
||||
|
||||
proxy_choice = input("\n请选择代理 (0 或 1, 默认为0): ").strip()
|
||||
if proxy_choice not in ['0', '1']:
|
||||
proxy_choice = '0'
|
||||
proxy_idx = int(proxy_choice)
|
||||
|
||||
await test_basic_browser(proxy_idx)
|
||||
|
||||
print(f"\n{'='*60}")
|
||||
print("✅ 测试完成!")
|
||||
print("="*60)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Windows环境下设置事件循环策略
|
||||
if sys.platform == 'win32':
|
||||
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
|
||||
|
||||
# 运行测试
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user