# -*- coding: utf-8 -*-
import sys
sys.stdout.reconfigure(encoding='utf-8')
import websocket, json, time

ws_url = 'ws://localhost:9222/devtools/page/AF7901AD02F8B4871EB42FDB587C2F9A'
ws = websocket.create_connection(ws_url)
ws.settimeout(10)

# 获取标题textarea的outerHTML
cmd = json.dumps({'id': 1, 'method': 'Runtime.evaluate', 'params': {
    'expression': 'document.getElementById("title") ? document.getElementById("title").outerHTML.substring(0, 200) : "not found"'}})
ws.send(cmd)
time.sleep(0.3)
result = ws.recv()
data = json.loads(result)
print('=== 标题元素HTML ===')
print(data.get('result', {}).get('result', {}).get('value', 'NO_DATA'))

# 检查页面是否iframe
cmd2 = json.dumps({'id': 2, 'method': 'Runtime.evaluate', 'params': {
    'expression': 'document.querySelector("iframe") ? "has iframe" : "no iframe"'}})
ws.send(cmd2)
time.sleep(0.3)
result2 = ws.recv()
data2 = json.loads(result2)
print('=== iframe检查 ===')
print(data2.get('result', {}).get('result', {}).get('value', 'NO_DATA'))

ws.close()
