72 lines
4.5 KiB
JavaScript
72 lines
4.5 KiB
JavaScript
/** Lazada 选类目:打开弹窗后搜一个关键词,dump 结果项结构(决定能不能复用 selectCategory) */
|
||
import { newPage, closePage, wait } from '/home/admin9webs/shein建品/sh_lib.mjs';
|
||
import { SAN as S } from '/tmp/cdp3.mjs';
|
||
|
||
const c = await newPage('https://www.dianxiaomi.com/web/lazada/add');
|
||
await wait(12000);
|
||
|
||
// 1) 选店铺(第一家)
|
||
const pos = JSON.parse(String(await c.evalJS(`(()=>{
|
||
const f=[...document.querySelectorAll('.ant-form-item')].filter(e=>e.offsetParent!==null)
|
||
.find(e=>/店铺/.test((e.querySelector('label')||{}).innerText||''));
|
||
const s=f?f.querySelector('.ant-select-selector'):null; if(!s) return '[]';
|
||
s.scrollIntoView({block:'center'}); const r=s.getBoundingClientRect();
|
||
return JSON.stringify([Math.round(r.x+r.width/2),Math.round(r.y+r.height/2)]);})()`)));
|
||
await c.send('Input.dispatchMouseEvent', { type: 'mouseMoved', x: pos[0], y: pos[1], buttons: 0 }); await wait(300);
|
||
for (const t of ['mousePressed', 'mouseReleased'])
|
||
await c.send('Input.dispatchMouseEvent', { type: t, x: pos[0], y: pos[1], button: 'left', clickCount: 1, buttons: 1 });
|
||
await wait(2500);
|
||
await c.evalJS(`(()=>{const dd=[...document.querySelectorAll('.ant-select-dropdown')].filter(e=>e.offsetParent!==null).pop();
|
||
const op=[...dd.querySelectorAll('.ant-select-item-option')][0]; const r=op.getBoundingClientRect();
|
||
for(const ev of ['mousedown','mouseup','click']) op.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window,clientX:r.x+r.width/2,clientY:r.y+r.height/2}));
|
||
return 1;})()`);
|
||
await wait(7000);
|
||
|
||
// 2) 打开「选择分类」弹窗
|
||
console.log('打开:', String(await c.evalJS(`(()=>{
|
||
const it=[...document.querySelectorAll('.ant-form-item')].filter(e=>e.offsetParent!==null).find(e=>{
|
||
const t=(e.querySelector('label')||{}).innerText||'';
|
||
return /分类/.test(t) && !/店小秘/.test(t) && /选择分类/.test(e.innerText||'');});
|
||
if(!it) return 'no-item';
|
||
const b=[...it.querySelectorAll('*')].find(e=>(e.innerText||'').trim()==='选择分类'&&e.children.length===0);
|
||
if(!b) return 'no-btn';
|
||
for(const ev of ['mousedown','mouseup','click']) b.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window}));
|
||
return 'opened';})()`)));
|
||
await wait(4000);
|
||
|
||
// 3) 输关键词
|
||
console.log('输入:', String(await c.evalJS(`(()=>{
|
||
const m=[...document.querySelectorAll('.ant-modal')].filter(e=>e.offsetParent!==null).pop();
|
||
if(!m) return 'no-modal';
|
||
const inp=m.querySelector('input[placeholder="搜索分类名称"]')||m.querySelector('input');
|
||
if(!inp) return 'no-input';
|
||
const setter=Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype,'value').set;
|
||
setter.call(inp, '发圈'); inp.dispatchEvent(new Event('input',{bubbles:true})); inp.dispatchEvent(new Event('change',{bubbles:true}));
|
||
return 'typed:'+inp.value;})()`)));
|
||
await wait(900);
|
||
|
||
// 4) 点搜索
|
||
console.log('按钮:', String(await c.evalJS(`(()=>{
|
||
const m=[...document.querySelectorAll('.ant-modal')].filter(e=>e.offsetParent!==null).pop();
|
||
if(!m) return 'no-modal';
|
||
const bs=[...m.querySelectorAll('button')].map(b=>(b.innerText||'').trim()).filter(Boolean);
|
||
const b=[...m.querySelectorAll('button')].find(x=>/搜索/.test(x.innerText||''));
|
||
if(b) for(const ev of ['mousedown','mouseup','click']) b.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window}));
|
||
return JSON.stringify(bs);})()`)));
|
||
await wait(5000);
|
||
|
||
// 5) dump 结果结构
|
||
const d = await c.evalJS(`(()=>{
|
||
const m=[...document.querySelectorAll('.ant-modal')].filter(e=>e.offsetParent!==null).pop();
|
||
if(!m) return JSON.stringify({modal:'gone'});
|
||
const cls={}; m.querySelectorAll('*').forEach(e=>{const k=String(e.className||''); if(k&&k.length<40) cls[k]=(cls[k]||0)+1;});
|
||
const sr=[...m.querySelectorAll('.search-result-item')].map(e=>(e.innerText||'').replace(/\\s+/g,' ').trim().slice(0,60));
|
||
const items=[...m.querySelectorAll('.categories-item')].map(e=>(e.innerText||'').replace(/\\s+/g,' ').trim().slice(0,50)).slice(0,6);
|
||
const leaves=[...m.querySelectorAll('*')].filter(e=>e.children.length===0&&/发圈|Scrunch|Hair/i.test(e.innerText||'')).map(e=>String(e.className).slice(0,30)+'|'+(e.innerText||'').trim().slice(0,40)).slice(0,6);
|
||
return JSON.stringify({top:Object.entries(cls).sort((a,b)=>b[1]-a[1]).slice(0,10), srCount:sr.length, sr:sr.slice(0,3), items, leaves,
|
||
tail:(m.innerText||'').replace(/\\s+/g,' ').trim().slice(0,180)});})()`);
|
||
console.log('结果:', S(String(d)).slice(0, 1300));
|
||
try { await c.shot('/tmp/lz_cat3.jpg'); console.log('shot ok'); } catch (e) {}
|
||
await closePage(c);
|
||
process.exit(0);
|