"""十一探测:右侧面板是否 iframe / 可信点击是否生效 / 沟通点击后的弹窗""" import json import time from pathlib import Path import websocket from client.browser.cdp_lite import LiteCDP from client.browser.manager import BrowserManager OUT = Path(__file__).resolve().parent / "data" / "debug" / "job_page_probe11.txt" _lines: list[str] = [] def log(*a): s = " ".join(str(x) for x in a) _lines.append(s) OUT.write_text("\n".join(_lines), encoding="utf-8") class Session: def __init__(self, ws_url): self.ws = websocket.create_connection(ws_url, timeout=30) self._id = 0 def call(self, method, params=None, timeout=10): self._id += 1 mid = self._id self.ws.send(json.dumps({"id": mid, "method": method, "params": params or {}})) self.ws.settimeout(timeout) while True: msg = json.loads(self.ws.recv()) if msg.get("id") == mid: return msg.get("result") or {} def eval_js(self, expr, timeout=10): return (self.call("Runtime.evaluate", {"expression": expr, "returnByValue": True}, timeout) .get("result", {}).get("value")) def click_at(self, x, y): self.call("Input.dispatchMouseEvent", { "type": "mousePressed", "x": x, "y": y, "button": "left", "clickCount": 1}) time.sleep(0.08) self.call("Input.dispatchMouseEvent", { "type": "mouseReleased", "x": x, "y": y, "button": "left", "clickCount": 1}) def close(self): try: self.ws.close() except Exception: pass SEARCH_JS = """(() => { const inp = document.querySelector('input[placeholder*="搜索"]'); if (!inp) return 'NO_INPUT'; const setter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set; setter.call(inp, 'java'); inp.dispatchEvent(new Event('input', {bubbles: true})); inp.dispatchEvent(new KeyboardEvent('keydown', {key: 'Enter', code: 'Enter', keyCode: 13, which: 13, bubbles: true})); return 'OK'; })()""" def main(): BrowserManager().ensure_browser_started() lite = LiteCDP() tab = lite.open_tab("https://www.zhipin.com/web/geek/jobs") time.sleep(8) s = Session(tab["webSocketDebuggerUrl"]) s.eval_js(SEARCH_JS) time.sleep(5) # 1) iframe 检查 r1 = s.eval_js("""(() => [...document.querySelectorAll('iframe')] .map(f => ({src: (f.src || '').slice(0, 120), cls: f.className, w: f.offsetWidth, h: f.offsetHeight})))()""") log("== 1) iframes ==", r1) log("== 1b) targets ==", [(t.get("type"), t.get("url", "")[:100]) for t in lite.list_tabs()]) # 2) 可信点击第三张卡片,验证 Input 域生效 xy = s.eval_js("""(() => { const c = document.querySelectorAll('.job-card-wrap')[2]; if (!c) return null; c.scrollIntoView({block: 'center'}); const r = c.getBoundingClientRect(); return JSON.stringify({x: r.x + r.width / 2, y: r.y + r.height / 2}); })()""") time.sleep(0.6) xy = s.eval_js("""(() => { const c = document.querySelectorAll('.job-card-wrap')[2]; const r = c.getBoundingClientRect(); return JSON.stringify({x: r.x + r.width / 2, y: r.y + r.height / 2}); })()""") p = json.loads(xy) s.click_at(int(p["x"]), int(p["y"])) time.sleep(2) r2 = s.eval_js("""(() => { const cards = [...document.querySelectorAll('.job-card-wrap')]; return JSON.stringify({activeIndex: cards.findIndex(c => c.className.includes('active'))}); })()""") log("== 2) 可信点击卡片后 activeIndex(应为2) ==", r2) # 3) 可信点击沟通按钮 + 弹窗检查 xy2 = s.eval_js("""(() => { const b = document.querySelector('.op-btn-chat'); if (!b) return null; const r = b.getBoundingClientRect(); return JSON.stringify({x: r.x + r.width / 2, y: r.y + r.height / 2}); })()""") log("== 3) 沟通按钮坐标 ==", xy2) before = {t["id"] for t in lite.list_tabs()} if xy2: q = json.loads(xy2) s.click_at(int(q["x"]), int(q["y"])) time.sleep(4) r3 = s.eval_js("""(() => { const dlg = document.querySelector('.dialog-container, .toast-container, [class*="dialog-wrap"]'); return JSON.stringify({url: location.href, dialog: dlg && dlg.offsetHeight > 0 ? dlg.innerText.slice(0, 200) : null}); })()""") new_tabs = [t for t in lite.list_tabs() if t["id"] not in before and t["id"] != tab["id"]] log("== 3b) 点击后 ==", r3, " 新标签 ==", [t.get("url") for t in new_tabs]) log("== 3c) targets after ==", [(t.get("type"), t.get("url", "")[:100]) for t in lite.list_tabs()]) s.close() lite.close_tab(tab["id"]) for t in new_tabs: lite.close_tab(t["id"]) log("== DONE ==") print("probe11 done ->", OUT) if __name__ == "__main__": main()