File size: 2,609 Bytes
8c28d25
 
bc77c74
 
8c28d25
 
 
bc77c74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c28d25
bc77c74
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import asyncio, os
import subprocess, sys
from playwright.async_api import async_playwright

# install browser binary first
subprocess.run([sys.executable, "-m", "playwright", "install", "chromium"], check=True, capture_output=True)

S = "https://bep40-vai-avatar2.hf.space"

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True, args=["--no-sandbox","--disable-dev-shm-usage"])
        ctx = await browser.new_context(viewport={"width":1280,"height":900})
        page = await ctx.new_page()
        await page.goto(S, wait_until="domcontentloaded", timeout=60000)
        await page.wait_for_timeout(6000)
        # open Catalogue AI (welcome button or window API)
        try:
            await page.evaluate("window.openCatalogueAI && window.openCatalogueAI()")
        except Exception as e:
            print("open err", str(e)[:100])
        await page.wait_for_timeout(5000)
        # type search
        try:
            await page.fill("#cai-q", "kệ nâng hạ")
            await page.click("#cai-sb")
        except Exception as e:
            print("fill err", str(e)[:100])
        await page.wait_for_timeout(4000)
        # screenshot whole page
        shot = await page.screenshot(full_page=False)
        os.makedirs("/tmp/shots", exist_ok=True)
        open("/tmp/shots/search.png","wb").write(shot)
        print("screenshot bytes:", len(shot))
        # also dump the carousel card texts + img src lengths (from DOM)
        info = await page.evaluate("""() => {
          const cards = [...document.querySelectorAll('#cai-car > div')];
          return cards.map(c => {
            const img = c.querySelector('img');
            const txt = c.innerText.split('\\n').slice(0,3).join(' | ');
            return { txt, imgLen: img ? (img.src.length) : 0, imgPrefix: img ? img.src.slice(0,30) : '' };
          });
        }""")
        for i, c in enumerate(info):
            print(f"card{i}: {c['txt']} imgLen={c['imgLen']} prefix={c['imgPrefix']}")
        # upload screenshot
        from huggingface_hub import HfApi
        api = HfApi(token=os.environ["HF_TOKEN"], endpoint="https://huggingface.co")
        api.upload_file(path_or_fileobj=shot, path_in_repo="debug_live/browser_search.png",
            repo_id="bep40/grob-products-updated", repo_type="dataset",
            commit_message="headless browser search kệ nâng hạ screenshot")
        await browser.close()

subprocess.run([sys.executable, "-m", "playwright", "install", "chromium"], check=True, capture_output=True)
asyncio.run(main())
print("DONE")