import requests
import time
from DrissionPage import ChromiumPage, ChromiumOptions

# ========== 配置 ==========
API_BASE_URL = "http://127.0.0.1:50326"

headers = {
    "Authorization": f"Bearer asp_174003986c9b0799677c5b2c1adb76e402735d753bc91a91",
    "Content-Type": "application/json"
}

# # ========== 步骤 1: 创建浏览器环境 ==========
# print("1. 创建浏览器环境...")
# response = requests.post(
#     f"{API_BASE_URL}/api/browser/create",
#     headers=headers,
# params = {
#     "current": 1,
#     "pageSize": 20,
#     "keyword": "test"
# }
# )
#
# env_id = response.json()["data"]["envId"]
# print(f"   环境创建成功，ID: {env_id}")

# ========== 步骤 2: 启动浏览器 ==========
print("2. 启动浏览器...")
response = requests.post(
    f"{API_BASE_URL}/api/browser/start",
    json={"envId": 146473},
    headers=headers
)
#
# debug_port = response.json()["data"]["port"]
# print(f"   浏览器已启动，调试端口: {debug_port}")
#
# # ========== 步骤 3: 连接 DrissionPage ==========
# print("3. 连接 DrissionPage...")
# time.sleep(3)  # 等待浏览器完全启动
#
# # 方式1: 通过端口连接（推荐）
# co = ChromiumOptions().set_local_port(debug_port)
# page = ChromiumPage(addr_or_opts=co)
#
# # 方式2: 直接使用端口号
# # from DrissionPage import Chromium
# # page = Chromium(debug_port)
#
# # 方式3: 使用地址:端口
# # page = Chromium(f'127.0.0.1:{debug_port}')
#
# # 方式4: 使用 WebSocket URL（需要先获取 ws_url）
# # ws_url = response.json()["data"]["ws"]
# # page = Chromium(ws_url)
#
# print("   DrissionPage 连接成功")
#
# # ========== 步骤 4: 执行自动化操作 ==========
# print("4. 执行自动化操作...")
#
# # 访问网页
# page.get('https://www.example.com')
# print(f"   当前页面: {page.title}")
#
# # 更多操作示例
# page.get_screenshot('screenshot.png')  # 截图
# print("   已保存截图")
#
# print("\n完成！")