# -*- 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)

# 检查标题输入框
cmd = json.dumps({'id': 1, 'method': 'Runtime.evaluate', 'params': {
    'expression': '''
    (function() {
        var title = document.getElementById("title");
        if (title) {
            return "value: " + title.value + ", placeholder: " + title.placeholder;
        }
        return "NO_TITLE_ELEMENT";
    })()
    '''}})
ws.send(cmd)
time.sleep(0.3)
result = ws.recv()
data = json.loads(result)
print('=== 标题输入框 ===')
print(data.get('result', {}).get('result', {}).get('value', 'NO_DATA'))

# 检查页面可见文本
cmd2 = json.dumps({'id': 2, 'method': 'Runtime.evaluate', 'params': {
    'expression': 'document.body.innerText.substring(0, 800)'}})
ws.send(cmd2)
time.sleep(0.3)
result2 = ws.recv()
data2 = json.loads(result2)
print('=== 页面文本 ===')
print(data2.get('result', {}).get('result', {}).get('value', 'NO_DATA')[:800])

ws.close()
