基本完成
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
"""验证城市弹窗选择是否生效:打开弹窗 → 选上海 → 确认切换 → 切回北京"""
|
||||
import json
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from client.browser.cdp_lite import LiteCDP
|
||||
from client.browser.manager import BrowserManager
|
||||
|
||||
OUT = Path(__file__).resolve().parent / "data" / "debug" / "城市选择验证.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 会话:
|
||||
def __init__(self, ws_url):
|
||||
import websocket
|
||||
self.ws = websocket.create_connection(ws_url, timeout=30)
|
||||
self._id = 0
|
||||
|
||||
def 求值(self, expr, timeout=10):
|
||||
self._id += 1
|
||||
mid = self._id
|
||||
self.ws.send(json.dumps({"id": mid, "method": "Runtime.evaluate",
|
||||
"params": {"expression": expr, "returnByValue": True}}))
|
||||
self.ws.settimeout(timeout)
|
||||
while True:
|
||||
msg = json.loads(self.ws.recv())
|
||||
if msg.get("id") == mid:
|
||||
return (msg.get("result") or {}).get("result", {}).get("value")
|
||||
|
||||
def 关闭(self):
|
||||
try:
|
||||
self.ws.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def 选城市脚本(名称: str) -> str:
|
||||
return "((name) => (() => {" \
|
||||
"const dlg = document.querySelector('.city-select-dialog');" \
|
||||
"if (!dlg || dlg.offsetHeight === 0) return JSON.stringify({ok: false, reason: '弹窗未打开'});" \
|
||||
"let li = [...dlg.querySelectorAll('.city-list-hot li, .city-list li')].find(l => (l.innerText || '').trim() === name);" \
|
||||
"if (!li) { for (const t of [...dlg.querySelectorAll('.city-char-list li')]) { t.click();" \
|
||||
" li = [...dlg.querySelectorAll('.city-list-hot li, .city-list li')].find(l => (l.innerText || '').trim() === name);" \
|
||||
" if (li) break; } }" \
|
||||
"if (!li) return JSON.stringify({ok: false, reason: '未找到城市'});" \
|
||||
"li.click(); return JSON.stringify({ok: true});" \
|
||||
"})())(" + json.dumps(名称, ensure_ascii=False) + ")"
|
||||
|
||||
|
||||
def main():
|
||||
BrowserManager().ensure_browser_started()
|
||||
lite = LiteCDP()
|
||||
|
||||
tab = lite.open_tab("https://www.zhipin.com/web/geek/jobs")
|
||||
time.sleep(8)
|
||||
s = 会话(tab["webSocketDebuggerUrl"])
|
||||
|
||||
log("1) 当前城市:", s.求值("(document.querySelector('.cur-city-label') || {}).innerText || ''"))
|
||||
|
||||
s.求值("(document.querySelector('.city-label') || {click: () => {}}).click()")
|
||||
time.sleep(1.5)
|
||||
log("2) 弹窗已打开:", s.求值(
|
||||
"!!document.querySelector('.city-select-dialog') && document.querySelector('.city-select-dialog').offsetHeight > 0"))
|
||||
|
||||
log("3) 点击上海:", s.求值(选城市脚本("上海")))
|
||||
time.sleep(4)
|
||||
log("3b) 切换后:", s.求值("""JSON.stringify({
|
||||
城市: (document.querySelector('.cur-city-label') || {}).innerText || '',
|
||||
地址: location.href,
|
||||
卡片数: document.querySelectorAll('.job-card-wrap').length})"""))
|
||||
|
||||
# 切回北京,恢复用户原状态
|
||||
s.求值("(document.querySelector('.city-label') || {click: () => {}}).click()")
|
||||
time.sleep(1.5)
|
||||
log("4) 点击北京:", s.求值(选城市脚本("北京")))
|
||||
time.sleep(2)
|
||||
|
||||
s.关闭()
|
||||
lite.close_tab(tab["id"])
|
||||
log("完成")
|
||||
print("验证完成 ->", OUT)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user